<?php


namespace Drupal\shushengyuan;

use Drupal\distribution\Entity\Distribution;
use Drupal\s_commerce_service\Entity\SCommerceService;
use Drupal\s_commerce_service\Entity\SCommerceServiceLine;
use Drupal\services_facetapi\SearchApiFuncTrait;
use Drupal\shushengyuan\Schemas\Anchor;
use Drupal\shushengyuan\Schemas\AnnouncementItem;
use Drupal\shushengyuan\Schemas\ArticleItem;
use Drupal\shushengyuan\Schemas\Banner;
use Drupal\shushengyuan\Schemas\CalendarItem;
use Drupal\shushengyuan\Schemas\CalendarWrapper;
use Drupal\shushengyuan\Schemas\CashItem;
use Drupal\shushengyuan\Schemas\CategoryIndexItem;
use Drupal\shushengyuan\Schemas\CategoryItemGroup;
use Drupal\shushengyuan\Schemas\CityItem;
use Drupal\shushengyuan\Schemas\CommentItem;
use Drupal\shushengyuan\Schemas\CouponfreeItem;
use Drupal\shushengyuan\Schemas\CouponHoldItem;
use Drupal\shushengyuan\Schemas\CouponItem;
use Drupal\shushengyuan\Schemas\CouponItemProduct;
use Drupal\shushengyuan\Schemas\ErrandItem;
use Drupal\shushengyuan\Schemas\ErrandOrder;
use Drupal\shushengyuan\Schemas\ErrandOrderInfo;
use Drupal\shushengyuan\Schemas\ExpertInfo;
use Drupal\shushengyuan\Schemas\ExpertItem;
use Drupal\shushengyuan\Schemas\ExpertType;
use Drupal\shushengyuan\Schemas\FavoriteProductItem;
use Drupal\shushengyuan\Schemas\HotelItem;
use Drupal\shushengyuan\Schemas\HotelItemDetail;
use Drupal\shushengyuan\Schemas\HotelRoomCalendar;
use Drupal\shushengyuan\Schemas\IncomeOrderItem;
use Drupal\shushengyuan\Schemas\IndexProductsBoxItem;
use Drupal\shushengyuan\Schemas\LineItemProduct;
use Drupal\shushengyuan\Schemas\MsProductItem;
use Drupal\shushengyuan\Schemas\MyGiftCard;
use Drupal\shushengyuan\Schemas\MyInnerTeam;
use Drupal\shushengyuan\Schemas\MyOrderItem;
use Drupal\shushengyuan\Schemas\MyServiceInfo;
use Drupal\shushengyuan\Schemas\MyServiceItem;
use Drupal\shushengyuan\Schemas\MyStoreOrderItem;
use Drupal\shushengyuan\Schemas\MyStorePerformanceItem;
use Drupal\shushengyuan\Schemas\MyStoreProductItem;
use Drupal\shushengyuan\Schemas\MyStoreServiceOrderItem;
use Drupal\shushengyuan\Schemas\MyTeamAchievement;
use Drupal\shushengyuan\Schemas\MyTeamItem;
use Drupal\shushengyuan\Schemas\MyTaxationItem;
use Drupal\shushengyuan\Schemas\MyTeamStore;
use Drupal\shushengyuan\Schemas\OrderCart;
use Drupal\shushengyuan\Schemas\OrderInfo;
use Drupal\shushengyuan\Schemas\OrderLogistics;
use Drupal\shushengyuan\Schemas\OrderPayment;
use Drupal\shushengyuan\Schemas\OrderServiceInfo;
use Drupal\shushengyuan\Schemas\OrderServiceItem;
use Drupal\shushengyuan\Schemas\PlantGrassCommentsItem;
use Drupal\shushengyuan\Schemas\PlantGrassInfo;
use Drupal\shushengyuan\Schemas\PlantGrassItem;
use Drupal\shushengyuan\Schemas\PointExchangeOrder;
use Drupal\shushengyuan\Schemas\PointItem;
use Drupal\shushengyuan\Schemas\PointItemGold;
use Drupal\shushengyuan\Schemas\PointProductInfo;
use Drupal\shushengyuan\Schemas\PointProductItem;
use Drupal\shushengyuan\Schemas\ProductItem;
use Drupal\shushengyuan\Schemas\ProductItemInfo;
use Drupal\shushengyuan\Schemas\ProductPtingItem;
use Drupal\shushengyuan\Schemas\ProductPurchaseItem;
use Drupal\shushengyuan\Schemas\PtProductItem;
use Drupal\shushengyuan\Schemas\SearchAutocomplete;
use Drupal\shushengyuan\Schemas\SignItem;
use Drupal\shushengyuan\Schemas\StoreInfo;
use Drupal\shushengyuan\Schemas\StoreItem;
use Drupal\shushengyuan\Schemas\SweepstakeConfig;
use Drupal\shushengyuan\Schemas\SweepstakeLog;
use Drupal\shushengyuan\Schemas\TopicItem;
use Drupal\shushengyuan\Schemas\User;
use Drupal\shushengyuan\Schemas\VipItem;
use Drupal\shushengyuan\Schemas\WindowItem;
use Drupal\shushengyuan\Traits\ServicesControllerTrait;
use Drupal\shusy_banner\Entity\ShusyBanner;
use Drupal\shusy_category\Entity\ShusyCategory;
use Drupal\shusy_city\Entity\ShusyCity;
use Drupal\shusy_city_video\Entity\ShusyCityVideo;
use Drupal\shusy_couponfree\Entity\ShusyCouponfree;
use Drupal\shusy_distribution\Entity\ShusyDistribution;
use Drupal\shusy_errand\Entity\ShusyErrand;
use Drupal\shusy_errand\Entity\ShusyErrandOrder;
use Drupal\shusy_gift_card\Entity\ShusyGiftCard;
use Drupal\shusy_index_category\Entity\ShusyIndexCategory;
use Drupal\shusy_product_category\Entity\ShusyProductCategory;
use Drupal\shusy_promote\Entity\ShusyPromote;
use Drupal\shusy_service\Entity\ShusyService;
use Drupal\shusy_sign\Entity\ShusySign;
use Drupal\shusy_store\Entity\ShusyStore;
use Drupal\shusy_store_order\Entity\ShusyStoreOrder;
use Drupal\shusy_store_order_address\Entity\ShusyStoreOrderAddress;
use Drupal\shusy_store_order_line\Entity\ShusyStoreOrderLine;
use Drupal\shusy_store_order_shipping\Entity\ShusyStoreOrderShipping;
use Drupal\shusy_store_performance\Entity\ShusyStorePerformanceTotal;
use Drupal\shusy_sweepstake\Entity\ShusySweepstake;
use Drupal\shusy_team_apply\Entity\ShusyTeamApply;
use Drupal\shusy_team_company_apply\Entity\ShusyTeamCompanyApply;
use Drupal\shusy_team_stats\Entity\ShusyTeamStats;
use Drupal\shusy_vip\Entity\ShusyVip;
use Drupal\shusy_vip\Entity\ShusyVipBind;
use Drupal\ssy_index_window\Entity\SsyIndexWindow;
use EntityFieldQuery;
use EntityListWrapper;
use Exception;
use SearchApiException;

/**
 * @OA\Info(
 *   title="API文档",
 *   version="1.0.0",
 *   description="https://shushengyuan.sccxbe.com/services/mini"
 * )
 * @OA\Server(
 *   url="https://shushengyuan.sccxbe.com/services/mini",
 *   description="description"
 * )
 * //securitySchemes: { mini_token: { type: apiKey, in: header, name:
 *   Services-Mini-Token, description: api调用的身份认证token } }
 * @OA\SecurityScheme(
 *   securityScheme="mini_token",
 *   type="apiKey",
 *   in="header",
 *   name="Services-Mini-Token"
 * )
 */
class ShushengyuanServicesRetrieveController {

  use ServicesControllerTrait;
  use SearchApiFuncTrait;

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_live_anchor",
   *   summary="直播广场 common/load_live_anchor",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="直播主播",
   *         @OA\Items(ref="#/components/schemas/Anchor")
   *       ),
   *     )
   *   )
   * )
   */
  function load_live_anchor(int $offset, int $limit) {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_anchor');
    $query->propertyCondition('status', 1);
    $query->propertyOrderBy('sort');
    $query->propertyOrderBy('id');
    $query->range($offset, $limit);
    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_anchor'] as $item) {
        $list[] = Anchor::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_plant_grass",
   *   summary="种草列表 common/load_plant_grass",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="种草列表",
   *         @OA\Items(ref="#/components/schemas/PlantGrassItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_plant_grass(int $offset, int $limit) {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_plant_grass');
    $query->propertyCondition('status', 1);
    $query->propertyOrderBy('created', 'DESC');
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_plant_grass'] as $item) {
        $list[] = PlantGrassItem::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/load_plant_grass_info",
   *   summary="种草详情 common/load_plant_grass_info",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="PlantGrassItem.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="info",
   *         type="object",
   *         ref="#/components/schemas/PlantGrassInfo"
   *       ),
   *     )
   *   )
   * )
   */
  function load_plant_grass_info(int $id) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'info' => PlantGrassInfo::get($id),
    ];
  }

  /**
   * @param string $keys
   * @return array
   * @OA\Get(
   *   path="/common/search_autocomplete_topic",
   *   summary="话题搜索联想 common/search_autocomplete_topic",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="keys",
   *     description="搜索关键字",
   *     @OA\Schema(
   *       type="string",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="联想词",
   *         @OA\Items(ref="#/components/schemas/SearchAutocomplete")
   *       ),
   *     )
   *   )
   * )
   */
  function search_autocomplete_topic($keys = '') {
    $search = search_api_autocomplete_search_load('services_facetapi_topic');
    $fields = '-';
    $ret = [];
    try {
      if ($search->supportsAutocompletion()) {
        module_load_include('inc', 'search_api_autocomplete', 'search_api_autocomplete.pages');
        // Transliterate keys if applicable.
        if (_search_api_autocomplete_index_uses_translation($search->index())) {
          $keys = transliteration_get($keys);
        }
        [$complete, $incomplete] = $search->splitKeys($keys);
        $query = $search->getQuery($complete, $incomplete);
        if ($query) {
          $query->range(0, 10);
          $query->setOption('search id', 'search_api_autocomplete:' . $search->machine_name);
          if (!empty($search->options['fields'])) {
            $query->fields($search->options['fields']);
          }
          elseif ($fields != '-') {
            $fields = explode(' ', $fields);
            $query->fields($fields);
          }
          $query->preExecute();
          $suggestions = $search->getSuggester()
            ->getAutocompleteSuggestions($query, $incomplete, $keys);
          if ($suggestions) {
            foreach ($suggestions as $suggestion) {
              // Convert suggestion strings into an array.
              if (is_string($suggestion)) {
                $pos = strpos($suggestion, $keys);
                if ($pos === FALSE) {
                  $suggestion = [
                    'user_input' => '',
                    'suggestion_suffix' => $suggestion,
                  ];
                }
                else {
                  $suggestion = [
                    'suggestion_prefix' => substr($suggestion, 0, $pos),
                    'user_input' => $keys,
                    'suggestion_suffix' => substr($suggestion, $pos + strlen($keys)),
                  ];
                }
              }
              // Add defaults.
              $suggestion += [
                'url' => NULL,
                'keys' => NULL,
                'prefix' => NULL,
                'suggestion_prefix' => '',
                'user_input' => $keys,
                'suggestion_suffix' => '',
                'results' => NULL,
              ];
              if (empty($search->options['results'])) {
                unset($suggestion['results']);
              }

              // Decide what the action of the suggestion is – entering specific
              // search terms or redirecting to a URL.
              if (isset($suggestion['url'])) {
                $key = ' ' . $suggestion['url'];
              }
              else {
                // Also set the "keys" key so it will always be available in alter
                // hooks and the theme function.
                if (!isset($suggestion['keys'])) {
                  $suggestion['keys'] = $suggestion['suggestion_prefix'] . $suggestion['user_input'] . $suggestion['suggestion_suffix'];
                }
                $key = trim($suggestion['keys']);
              }

              if (!isset($ret[$key])) {
                $ret[$key] = $suggestion;
              }
            }

            $alter_params = [
              'query' => $query,
              'search' => $search,
              'incomplete_key' => $incomplete,
              'user_input' => $keys,
            ];
            drupal_alter('search_api_autocomplete_suggestions', $ret, $alter_params);
          }
        }
      }
    } catch (SearchApiException $e) {
      watchdog_exception('search_api_autocomplete', $e, '%type while retrieving autocomplete suggestions: !message in %function (line %line of %file).');
    }
    $list = [];
    foreach ($ret as $item) {
      $list[] = SearchAutocomplete::get(FALSE, FALSE, [$item]);
    }
    return [
      'list' => $list,
      'code' => 0,
      'msg' => 'ok',
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @param string $keys
   * @param array $sort
   * @return array
   * @throws \SearchApiException
   * @OA\Get(
   *   path="/common/search_topic",
   *   summary="搜索话题 common/search_topic",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="keys",
   *     description="搜索关键字",
   *     @OA\Schema(
   *       type="string",
   *     ),
   *     in="query",
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="话题",
   *         @OA\Items(ref="#/components/schemas/TopicItem")
   *       ),
   *     )
   *   )
   * )
   */
  function search_topic(int $offset, int $limit, $keys = '', $sort = []) {
    $filter = [
      '~and' => [
        //启用的话题
        'status' => 1,
      ],
    ];
    $result = $this->_searchApiResourceRetrieve('topic', $keys, $filter, $sort, $limit, $offset);
    $list = [];
    foreach ($result['results'] as $id => $item) {
      $list[] = TopicItem::get($id);
    }
    $facets = [];
    foreach ($result['facets'] as $facet) {
      if (!empty($facet['items'])) {
        $facets[] = [
          'items' => $facet['items'],
          'title' => $facet['title'],
        ];
      }
    }
    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'list' => $list,
      'count' => (int) $result['result count'],
      'facets' => $facets,
      'sorts' => $result['sorts'],
    ];
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/loadHotTopic",
   *   summary="获取热门话题 common/loadHotTopic",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v2.2"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="热门话题",
   *         @OA\Items(ref="#/components/schemas/TopicItem")
   *       ),
   *     )
   *   )
   * )
   */
  function loadHotTopic() {
    //获取热门话题
    $query = db_select('shusy_hot_topic', 'h');
    $query->innerJoin('shusy_topic', 't', 't.id=h.topic_id');
    $query->fields('h', ['id', 'topic_id']);
    $query->condition('t.status', 1);
    $query->orderBy('h.number', 'DESC');
    $query->orderBy('h.id');
    $query->range(0, 20);
    $result = $query->execute();
    $list = [];
    while ($row = $result->fetchAssoc()) {
      $list[] = TopicItem::init($row['topic_id']);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_my_favorite_products",
   *   summary="获取我收藏的产品 common/load_my_favorite_products",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="收藏的产品列表",
   *         @OA\Items(ref="#/components/schemas/FavoriteProductItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_my_favorite_products(int $offset, int $limit) {
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_favorite_product');
    $query->propertyCondition('status', 1);
    $query->propertyCondition('uid', $uid);
    $query->propertyOrderBy('changed', 'DESC');
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_favorite_product'] as $item) {
        $list[] = FavoriteProductItem::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param float $latitude
   * @param float $longitude
   * @return array
   * @OA\Get(
   *   path="/common/load_cities",
   *   summary="获取开通服务的城市列表 并返回距离最近的城市 common/load_cities",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="latitude",
   *     description="用户当前纬度",
   *     @OA\Schema(
   *       type="number",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="longitude",
   *     description="用户当前经度",
   *     @OA\Schema(
   *       type="number",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="city",
   *         type="object",
   *         description="用户所在城市 如果没有选择过 默认是距离最近的城市",
   *         ref="#/components/schemas/CityItem"
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="开通服务的城市列表",
   *         @OA\Items(ref="#/components/schemas/CityItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_cities(float $latitude, float $longitude) {
    $uid = $this->getUid();

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_city');
    $query->propertyCondition('status', 1);
    $query->propertyOrderBy('created', 'DESC');
    $result = $query->execute();
    $list = [];

    if (!empty($result)) {
      foreach ($result['shusy_city'] as $item) {
        $list[] = CityItem::get($item->id, FALSE, [$latitude, $longitude]);
      }
    }
    usort($list, 'drupal_sort_weight');
    $list = array_values($list);
    $city = $list[0] ?? FALSE;

    $cityColumns = [];
    foreach ($list as $item) {
      $cityColumns[] = $item['name'];
    }

    //检查缓存
    $cache = cache_get("in_city_{$uid}", 'cache_shushengyuan');
    if (!empty($cache) && $cache->expire >= REQUEST_TIME) {
      $city = CityItem::get($cache->data, FALSE, [$latitude, $longitude]);
    }
    else {
      if ($city != FALSE) {
        //保存24小时
        cache_set("in_city_{$uid}", $city['id'], 'cache_shushengyuan', REQUEST_TIME + (24 * 60 * 60));
      }
    }

    return [
      'code' => 0,
      'msg' => 'ok',
      'city' => $city,
      'list' => $list,
      'cityColumns' => $cityColumns,
      'location' => t("@name", ['@name' => $city['name']]),
    ];
  }

  /**
   * @return array
   * @throws \SearchApiException
   * @OA\Get(
   *   path="/common/load_index",
   *   summary="获取首页数据 common/load_index",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="banner",
   *         type="array",
   *         description="BANNER",
   *         @OA\Items(ref="#/components/schemas/BannerItem")
   *       ),
   *       @OA\Property(
   *         property="announcement",
   *         type="array",
   *         description="公告",
   *         @OA\Items(ref="#/components/schemas/AnnouncementItem")
   *       ),
   *       @OA\Property(
   *         property="categories",
   *         type="array",
   *         description="首页推荐分类",
   *         @OA\Items(ref="#/components/schemas/CategoryIndexItem")
   *       ),
   *       @OA\Property(
   *         property="ms",
   *         type="array",
   *         description="秒杀列表",
   *         @OA\Items(ref="#/components/schemas/MsProductItem")
   *       ),
   *       @OA\Property(
   *         property="pt",
   *         type="array",
   *         description="拼团列表",
   *         @OA\Items(ref="#/components/schemas/PtProductItem")
   *       ),
   *       @OA\Property(
   *         property="video",
   *         type="string",
   *         description="首页视频URL",
   *       ),
   *     )
   *   )
   * )
   */
  function load_index() {
    $uid = $this->getUid();
    //views 浏览量 不排重
    shusy_stats_record('views');
    //uids 访问量 排重
    $name = "shusy_stats_record_{$uid}";
    //00:00:00 分界
    $window = (strtotime($this->formatDate(REQUEST_TIME, 'Y-m-d') . ' 00:00:00') + (24 * 60 * 60)) - REQUEST_TIME;
    if (flood_is_allowed($name, 1, $window)) {
      shusy_stats_record('uids');
      flood_register_event($name, $window);
    }

    $city = $this->getCity($uid);

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 's_announcement');
    $query->propertyCondition('sticky', 1);
    $query->propertyCondition('status', 1);
    $query->propertyOrderBy('created', 'DESC');
    $query->propertyOrderBy('id', 'DESC');
    $result = $query->execute();
    $announcement = [];
    if (!empty($result)) {
      foreach ($result['s_announcement'] as $item) {
        $announcement[] = AnnouncementItem::get($item->id);
      }
    }

    $ms = $this->load_ms_products(0, 10);
    $pt = $this->load_pt_products(0, 10);

    return [
      'code' => 0,
      'msg' => 'ok',
      'banner' => $this->getBanner('index'),
      'announcement' => $announcement,
      'categories' => $this->getIndexCategories($city),
      'ms' => $ms['list'],
      'pt' => $pt['list'],
      'video' => '',
      'videos' => $this->getIndexVideo($city),
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_ms_products",
   *   summary="秒杀商品列表 common/load_ms_products",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="秒杀列表",
   *         @OA\Items(ref="#/components/schemas/MsProductItem")
   *       ),
   *     )
   *   )
   * )
   * @throws \SearchApiException
   */
  function load_ms_products(int $offset, int $limit) {
    $keys = '';
    $sort = [];
    $filter = [
      '~and' => [
        //显示上架的商品
        'status' => 1,
        //开启秒杀的商品
        'miaosha:status' => 1,
      ],
    ];
    $result = $this->_searchApiResourceRetrieve('products', $keys, $filter, $sort, $limit, $offset);
    $list = [];
    foreach ($result['results'] as $id => $item) {
      $list[] = MsProductItem::get($id);
    }
    $facets = [];
    foreach ($result['facets'] as $facet) {
      if (!empty($facet['items'])) {
        $facets[] = [
          'items' => $facet['items'],
          'title' => $facet['title'],
        ];
      }
    }
    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @throws \SearchApiException
   * @OA\Get(
   *   path="/common/load_pt_products",
   *   summary="获取拼团商品列表 common/load_pt_products",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="拼团列表",
   *         @OA\Items(ref="#/components/schemas/PtProductItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_pt_products(int $offset, int $limit) {
    $keys = '';
    $sort = [];
    $filter = [
      '~and' => [
        //显示上架的商品
        'status' => 1,
        //开启拼团的商品
        'pintuan:status' => 1,
      ],
    ];
    $result = $this->_searchApiResourceRetrieve('products', $keys, $filter, $sort, $limit, $offset);
    $list = [];
    foreach ($result['results'] as $id => $item) {
      $list[] = PtProductItem::get($id);
    }
    $facets = [];
    foreach ($result['facets'] as $facet) {
      if (!empty($facet['items'])) {
        $facets[] = [
          'items' => $facet['items'],
          'title' => $facet['title'],
        ];
      }
    }
    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'list' => $list,
    ];
  }

  protected function getBanner(string $putin) {
    $es = entity_load('shusy_banner', FALSE, [
      'status' => 1,
      'putin' => $putin,
    ]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyBanner) {
        return Banner::get($e->id);
      }
    }
    return [];
  }

  protected function getIndexCategories($city = NULL) {
    $es = shusy_index_category_load_multiple(FALSE, [
      'city_id' => 1,
      'status' => 1,
    ]);
    $list = [];
    foreach ($es as $e) {
      if ($e instanceof ShusyIndexCategory) {
        $list[] = CategoryIndexItem::get($e->id);
      }
    }
    return $list;
  }

  protected function getIndexVideo($city = NULL) {
    $list = [];
    $es = shusy_city_video_load_multiple(FALSE, [
      'status' => 1,
    ]);
    foreach ($es as $e) {
      if ($e instanceof ShusyCityVideo) {
        $ew = $e->wrapper();
        $field_video = $ew->field_video->value();
        $list[] = file_create_url($field_video['uri']);
      }
    }
    return $list;
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @throws \SearchApiException
   * @OA\Get(
   *   path="/common/load_index_products",
   *   summary="臻选好品 common/load_index_products",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="臻选好品",
   *         @OA\Items(ref="#/components/schemas/ProductItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_index_products(int $offset, int $limit) {
    $keys = '';
    $sort = [];
    //排除 拼团 秒杀等产品
    $category = variable_get_value('shushengyuan:index:category');
    $filter = [
      '~and' => [
        //显示上架的商品
        'status' => 1,
        'miaosha:status' => 0,
        'pintuan:status' => 0,
        //'categories:id' => $category,
        'recommend' => 1,
      ],
    ];
    $result = $this->_searchApiResourceRetrieve('products', $keys, $filter, $sort, $limit, $offset);
    $list = [];
    foreach ($result['results'] as $id => $item) {
      $tmp = ProductItem::get($id);
      $tmp['commisionTag'] = '';
      $list[] = $tmp;
    }
    $facets = [];
    foreach ($result['facets'] as $facet) {
      if (!empty($facet['items'])) {
        $facets[] = [
          'items' => $facet['items'],
          'title' => $facet['title'],
        ];
      }
    }
    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'list' => $list,
      //      'count' => (int) $result['result count'],
      //      'facets' => $facets,
      //      'sorts' => $result['sorts'],
    ];
  }

  /**
   * @param float $latitude
   * @param float $longitude
   * @param float $distance
   * @param string $keys
   * @return array
   * @throws \SearchApiException
   * @OA\Get(
   *   path="/common/load_stores",
   *   summary="附近店铺 common/load_stores",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="latitude",
   *     description="用户当前纬度",
   *     @OA\Schema(
   *       type="number",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="longitude",
   *     description="用户当前经度",
   *     @OA\Schema(
   *       type="number",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="distance",
   *     description="查询距离KM 默认返回50KM内的店铺",
   *     @OA\Schema(
   *       type="number",
   *       default="50"
   *     ),
   *     in="query",
   *     required=false,
   *   ),
   *   @OA\Parameter(
   *     name="keys",
   *     description="搜索用关键字",
   *     @OA\Schema(
   *       type="string",
   *     ),
   *     in="query",
   *     required=false,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="店铺列表",
   *         @OA\Items(ref="#/components/schemas/StoreItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_stores(float $latitude, float $longitude, float $distance = 50, string $keys = '') {
    $min_max_range = $this->getRange($latitude, $longitude, $distance);
    $uid = $this->getUid();
    $city = $this->getCity($uid);
    if ($city instanceof ShusyCity) {
      $cityDistance = $this->getDistance($latitude, $longitude, $city->latitude, $city->longitude);
      if ($cityDistance > $distance) {
        $latitude = $city->latitude;
        $longitude = $city->longitude;
      }
    }
    /**
     * '~gt' => '>',
     * '~lt' => '<',
     * '~gte' => '>=',
     * '~lte' => '<=',
     * '~ne' => '<>',
     */
    $filter = [
      '~and' => [
        'longitude' => [
          '~gt' => $min_max_range['minLon'],
          '~lt' => $min_max_range['maxLon'],
        ],
        'latitude' => [
          '~gt' => $min_max_range['minLat'],
          '~lt' => $min_max_range['maxLat'],
        ],
        //只显示审核通过的店铺
        'status' => 1,
      ],
    ];
    if ($city instanceof ShusyCity) {
      $filter = [
        '~and' => [
          //只显示审核通过的店铺
          'status' => 1,
          //按城市分布
          'city:id' => $city->id,
        ],
      ];
    }
    $sort = [];
    $result = $this->_searchApiResourceRetrieve('stores', $keys, $filter, $sort, NULL, 0);
    $list = [];
    foreach ($result['results'] as $id => $item) {
      $list[] = StoreItem::get($id, FALSE, [$latitude, $longitude]);
    }
    $facets = [];
    foreach ($result['facets'] as $facet) {
      if (!empty($facet['items'])) {
        $facets[] = [
          'items' => $facet['items'],
          'title' => $facet['title'],
        ];
      }
    }

    usort($list, 'drupal_sort_weight');

    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'list' => array_values($list),
    ];
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/load_store_info",
   *   summary="店铺信息 common/load_store_info",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="StoreItem.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="info",
   *         type="object",
   *         ref="#/components/schemas/StoreInfo"
   *       ),
   *     )
   *   )
   * )
   */
  function load_store_info(int $id) {
    shusy_store_stats_record($id, 'view');
    return [
      'code' => 0,
      'msg' => 'ok',
      'info' => StoreInfo::get($id),
    ];
  }

  /**
   * @param int $id
   * @param int $offset
   * @param int $limit
   * @return array
   * @throws \SearchApiException
   * @OA\Get(
   *   path="/common/load_store_products",
   *   summary="店铺商品 common/load_store_products",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="StoreItem.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="商品列表",
   *         @OA\Items(ref="#/components/schemas/ProductItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_store_products(int $id, int $offset, int $limit) {
    $store = shusy_store_load($id);
    if ($store instanceof ShusyStore && $store->status == 1) {
      $keys = '';
      $sort = [];
      $filter = [
        '~and' => [
          //显示上架的商品
          'status' => 1,
          'store:id' => $id,
        ],
      ];
      $result = $this->_searchApiResourceRetrieve('products', $keys, $filter, $sort, $limit, $offset);
      $list = [];
      foreach ($result['results'] as $id => $item) {
        $list[] = ProductItem::get($id);
      }
      $facets = [];
      foreach ($result['facets'] as $facet) {
        if (!empty($facet['items'])) {
          $facets[] = [
            'items' => $facet['items'],
            'title' => $facet['title'],
          ];
        }
      }
      return [
        'code' => (int) 0,
        'msg' => (string) 'ok',
        'list' => $list,
        'count' => (int) $result['result count'],
        'facets' => $facets,
        'sorts' => $result['sorts'],
      ];
    }
    return [
      'code' => 1,
      'msg' => '店铺不存在或审核中',
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @param string $keys
   * @param array $sort
   * @return array
   * @throws \SearchApiException
   * @OA\Get(
   *   path="/common/search_product",
   *   summary="搜索产品 common/search_product",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="keys",
   *     description="搜索用关键字",
   *     @OA\Schema(
   *       type="string",
   *     ),
   *     in="query",
   *     required=false,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="产品列表",
   *         @OA\Items(ref="#/components/schemas/ProductItem")
   *       ),
   *       @OA\Property(
   *         property="count",
   *         type="integer",
   *         description="搜索结果总数",
   *       ),
   *     )
   *   )
   * )
   */
  function search_product(int $offset, int $limit, string $keys = '', $sort = []) {
    $filter = [
      '~and' => [
        //显示上架的商品
        'status' => 1,
      ],
    ];
    $result = $this->_searchApiResourceRetrieve('products', $keys, $filter, $sort, $limit, $offset);
    $list = [];
    foreach ($result['results'] as $id => $item) {
      $list[] = ProductItem::get($id);
    }
    $facets = [];
    foreach ($result['facets'] as $facet) {
      if (!empty($facet['items'])) {
        $facets[] = [
          'items' => $facet['items'],
          'title' => $facet['title'],
        ];
      }
    }
    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'list' => $list,
      'count' => (int) $result['result count'],
      'facets' => $facets,
      'sorts' => $result['sorts'],
    ];
  }

  function searchStore(int $offset, int $limit, string $keys = '', $sort = []) {
    $filter = [
      '~and' => [
        //显示上架的商品
        'status' => 1,
      ],
    ];
    $result = $this->_searchApiResourceRetrieve('stores', $keys, $filter, $sort, $limit, $offset);
    $list = [];
    foreach ($result['results'] as $id => $item) {
      $list[] = StoreItem::get($id, FALSE);
    }
    $facets = [];
    foreach ($result['facets'] as $facet) {
      if (!empty($facet['items'])) {
        $facets[] = [
          'items' => $facet['items'],
          'title' => $facet['title'],
        ];
      }
    }
    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'list' => $list,
      'count' => (int) $result['result count'],
      'facets' => $facets,
      'sorts' => $result['sorts'],
    ];
  }

  /**
   * @param int $id
   * @param int $offset
   * @param int $limit
   * @return array
   * @throws \SearchApiException
   * @OA\Get(
   *   path="/common/load_product_by_category",
   *   summary="通过分类获取产品列表 common/load_product_by_category",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="CategoryIndexItem.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="产品列表",
   *         @OA\Items(ref="#/components/schemas/ProductItem")
   *       ),
   *       @OA\Property(
   *         property="count",
   *         type="integer",
   *         description="搜索结果总数",
   *       ),
   *     )
   *   )
   * )
   */
  function load_product_by_category(int $id, int $offset, int $limit) {
    $filter = [
      '~and' => [
        //显示上架的商品
        'status' => 1,
        'categories:id' => $id,
      ],
    ];
    $sort = [];
    $keys = '';
    $result = $this->_searchApiResourceRetrieve('products', $keys, $filter, $sort, $limit, $offset);
    $list = [];
    foreach ($result['results'] as $id => $item) {
      $list[] = ProductItem::get($id);
    }
    $facets = [];
    foreach ($result['facets'] as $facet) {
      if (!empty($facet['items'])) {
        $facets[] = [
          'items' => $facet['items'],
          'title' => $facet['title'],
        ];
      }
    }
    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'list' => $list,
      'count' => (int) $result['result count'],
      'facets' => $facets,
      'sorts' => $result['sorts'],
    ];
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/load_top_category",
   *   summary="获取一级分类 common/load_top_category",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="一级分类",
   *         @OA\Items(ref="#/components/schemas/CategoryItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_top_category() {
    $list = [];
    $query = db_select('shusy_category', 'c');
    $query->fields('c', ['id', 'name', 'weight', 'parent', 'status']);
    $query->condition('parent', 0);
    $query->condition('status', 1);
    $query->orderBy('weight', 'ASC');
    $query->orderBy('id');
    $result = $query->execute();
    while ($row = $result->fetchAssoc()) {
      $list[] = CategoryItemGroup::get(FALSE, FALSE, [
        $row['id'],
        $row['name'],
      ]);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/load_category",
   *   summary="获取分类 common/load_category",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="CategoryIndexItem.id 1级分类",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="分类分组",
   *         @OA\Items(ref="#/components/schemas/CategoryItemGroup")
   *       ),
   *     )
   *   )
   * )
   */
  function load_category(int $id) {
    $list = [];
    $query = db_select('shusy_category', 'c');
    $query->fields('c', ['id', 'name', 'weight', 'parent', 'status']);
    $query->condition('parent', $id);
    $query->condition('status', 1);
    $query->orderBy('weight', 'ASC');
    $query->orderBy('id');
    $result = $query->execute();
    while ($row = $result->fetchAssoc()) {
      $list[] = CategoryItemGroup::get(FALSE, FALSE, [
        $row['id'],
        $row['name'],
      ]);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/load_product_info",
   *   summary="获取商品详情 common/load_product_info",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="ProductItem.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="info",
   *         type="object",
   *         ref="#/components/schemas/ProductItemInfo"
   *       ),
   *     )
   *   )
   * )
   * 1 默认详情  2 秒杀详情 3 去拼团 4 拼团详情 5 平台供应
   */
  function load_product_info(int $id, int $type = 1) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'info' => ProductItemInfo::get($id, FALSE, [$type]),
      //评论计数
      'comment' => [
        'tags' => $this->getProductCommentTags($id),
      ],
    ];
  }

  function loadStorePermit(int $id) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'store' => StoreItem::init($id),
    ];
  }

  protected function getProductCommentTags(int $id) {
    $query = db_select('shusy_product_comment', 'c');
    $query->fields('c', ['id']);
    $query->condition('product_id', $id);
    $query->condition('status', 1);
    $query->orderBy('id');

    $s1Query = clone $query;
    $s1Query->condition('score', [4, 5], 'IN');

    $s2Query = clone $query;
    $s2Query->condition('score', [3], 'IN');

    $s3Query = clone $query;
    $s3Query->condition('score', [1, 2], 'IN');
    $s1 = $s1Query->execute()->rowCount();
    $s2 = $s2Query->execute()->rowCount();
    $s3 = $s3Query->execute()->rowCount();
    return [
      [
        'name' => t('全部(@number)', [
          '@number' => ($s1 + $s2 + $s3),
        ]),
      ],
      [
        'name' => t('好评(@number)', [
          '@number' => $s1,
        ]),
      ],
      [
        'name' => t('中评(@number)', [
          '@number' => $s2,
        ]),
      ],
      [
        'name' => t('差评(@number)', [
          '@number' => $s3,
        ]),
      ],
    ];
  }

  /**
   * @param int $id
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_product_pting",
   *   summary="获取产品正在拼的团 common/load_product_pting",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="ProductItem.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="正在拼的列表",
   *         @OA\Items(ref="#/components/schemas/ProductPtingItem")
   *       ),
   *       @OA\Property(
   *         property="total",
   *         type="integer",
   *         description="总正在拼的团数",
   *       ),
   *     )
   *   )
   * )
   */
  function load_product_pting(int $id, int $offset, int $limit) {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_pintuan');
    $query->propertyCondition('product_id', $id);
    $query->propertyCondition('status', [1], 'IN');
    //超5分钟
    $query->propertyCondition('validity', 300 + REQUEST_TIME, '>');
    $query->propertyOrderBy('id', 'DESC');
    $totalQuery = clone $query;
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_pintuan'] as $item) {
        $list[] = ProductPtingItem::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
      'total' => (int) $totalQuery->count()->execute(),
    ];
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/load_pting",
   *   summary="获取单个拼团信息 common/load_pting",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="ProductPtingItem.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="pting",
   *         type="object",
   *         ref="#/components/schemas/ProductPtingItem"
   *       ),
   *     )
   *   )
   * )
   */
  function load_pting(int $id) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'pting' => ProductPtingItem::get($id),
    ];
  }

  /**
   * @param int $id
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_product_comments",
   *   summary="产品评论列表 common/load_product_comments",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="ProductItem.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="评论",
   *         @OA\Items(ref="#/components/schemas/CommentItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_product_comments(int $id, int $offset, int $limit) {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_product_comment');
    $query->propertyCondition('product_id', $id);
    $query->propertyCondition('status', 1);
    $query->propertyOrderBy('created', 'DESC');
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_product_comment'] as $item) {
        $list[] = CommentItem::get($item->id);
      }
    }

    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/load_cart",
   *   summary="购物车 common/load_cart",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         @OA\Items(ref="#/components/schemas/OrderCart")
   *       ),
   *     )
   *   )
   * )
   */
  function load_cart() {
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_store_order');
    $query->propertyCondition('uid', $uid);
    $query->propertyCondition('status', 1);
    $query->propertyOrderBy('id', 'DESC');
    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_store_order'] as $item) {
        $list[] = OrderCart::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/load_payment_order",
   *   summary="订单结算信息 common/load_payment_order",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         @OA\Items(ref="#/components/schemas/OrderPayment")
   *       ),
   *       @OA\Property(
   *         property="address",
   *         type="object",
   *         description="选择的收货地址 如果没有选择地址返回 false",
   *         ref="#/components/schemas/AddressItem",
   *       ),
   *       @OA\Property(
   *         property="location",
   *         type="object",
   *         description="选择的经纬度地址 如果没有选择返回 false",
   *         ref="#/components/schemas/LocationItem",
   *       ),
   *       @OA\Property(
   *         property="errand",
   *         type="integer",
   *         description="是否存在快达订单",
   *       ),
   *       @OA\Property(
   *         property="total",
   *         type="number",
   *         description="总金额",
   *       ),
   *       @OA\Property(
   *         property="number",
   *         type="integer",
   *         description="总数量",
   *       ),
   *     )
   *   )
   * )
   */
  function load_payment_order() {
    $list = [];
    $uid = $this->getUid();
    $cid = "payment_{$uid}";
    $cache = $this->cacheGet($cid);
    $total = 0;
    $number = 0;
    $errand = 0;
    $show = ['address' => FALSE];

    //获取上一次使用的地址信息
    $defaultAddress = FALSE;
    $query = db_select('shusy_store_order_address', 'a');
    $query->fields('a', [
      'id',
      'username',
      'nationalcode',
      'telnumber',
      'provincename',
      'cityname',
      'countyname',
      'detailinfo',
      'postalcode',
    ]);
    $query->condition('uid', $uid);
    $query->orderBy('id', 'desc');
    $query->range(0, 1);
    $result = $query->execute()->fetchAssoc();
    if (!empty($result)) {
      /**
       * $e->username = $address['userName'];
       * $e->nationalcode = $address['nationalCode'];
       * $e->telnumber = $address['telNumber'];
       * $e->provincename = $address['provinceName'];
       * $e->cityname = $address['cityName'];
       * $e->countyname = $address['countyName'];
       * $e->detailinfo = $address['detailInfo'];
       * $e->postalcode = $address['postalCode'];
       */
      $defaultAddress = [
        'userName' => $result['username'],
        'nationalCode' => $result['nationalcode'],
        'telNumber' => $result['telnumber'],
        'provinceName' => $result['provincename'],
        'cityName' => $result['cityname'],
        'countyName' => $result['countyname'],
        'detailInfo' => $result['detailinfo'],
        'postalCode' => $result['postalcode'],
      ];
    }

    if (!empty($cache)) {
      foreach ($cache->data as $datum) {
        $item = OrderPayment::get($datum);

        if (!$item['address'] && $defaultAddress) {
          $this->switchAddress($datum, $defaultAddress);
        }
        //服务自动选择到店自提
        if (!$item['isService']) {
          $show['address'] = TRUE;
        }
        else {
          $this->switchShipping($datum, 0);
        }
        //如果只有一种可用物流服务 自动选择
        $item = OrderPayment::get($datum);
        if (count($item['shippingList']) == 1) {
          if ($item['shippingList'][0]['value'] == 2) {
            if ($item['address']) {
              $this->switchShipping($datum, $item['shippingList'][0]['value']);
            }
          }
          else {
            $this->switchShipping($datum, $item['shippingList'][0]['value']);
          }
        }

        $item = OrderPayment::get($datum);

        $total += $item['totalAmount'];
        $number += $item['qty'];
        /**
         * "配送方式 -1 未选择 0 到店自提 1 快达 2 快递",
         */
        switch ($item['shipping']) {
          case 1:
            $errand = 1;
            break;
        }
        $list[] = $item;
        //刷新订单支付有效期
        $this->refreshOrderValidity($datum);
      }
    }
    $this->cacheSet("inPayment_{$uid}", $cache->data);
    $address = FALSE;
    if (count($list) > 0) {
      $address = $list[0]['address'];
    }
    $location = FALSE;
    if (count($list) > 0) {
      $location = $list[0]['location'];
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
      'address' => $address,
      'location' => $location,
      'errand' => (int) $errand,
      //总金额
      'total' => $this->convertYuan($total),
      //总数量
      'number' => $number,
      'show' => $show,
      'ids' => [
        'vJksgYOPoiOCDl_fPck0_qKY2Mt1linKRQ7Z0oUWNTU',
        'hIIvKUK8IzBFC8XgxMVQHsKw8oj7CAL_7YWC2FGRirk',
        'HGp0ZE0QDBNnyM-VxeI0_zF9EaIIQzd9Hm9rsTqbmEk',
      ],
    ];
  }

  protected function clearShippingLine(ShusyStoreOrder $shusyStoreOrder) {
    $lines = $shusyStoreOrder->lines();
    foreach ($lines as $line) {
      if ($line instanceof ShusyStoreOrderLine && $line->type == 'shipping') {
        $line->status = 0;
        $line->save();
      }
    }
  }

  protected function switchAddress(int $id, array $address) {
    $order = shusy_store_order_load($id);
    if ($order instanceof ShusyStoreOrder) {
      //切换收货地址
      $es = shusy_store_order_address_load_multiple(FALSE, ['store_order_id' => $id]);
      if (!empty($es)) {
        $e = reset($es);
      }
      else {
        $e = new ShusyStoreOrderAddress();
        $e->store_order_id = $id;
      }
      $e->username = $address['userName'];
      $e->nationalcode = $address['nationalCode'];
      $e->telnumber = $address['telNumber'];
      $e->provincename = $address['provinceName'];
      $e->cityname = $address['cityName'];
      $e->countyname = $address['countyName'];
      $e->detailinfo = $address['detailInfo'];
      $e->postalcode = $address['postalCode'];
      $e->save();
    }
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/load_user_index",
   *   summary="我的 common/load_user_index",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="user",
   *         type="object",
   *         ref="#/components/schemas/User"
   *       ),
   *       @OA\Property(
   *         property="favorite_products",
   *         type="integer",
   *         description="收藏的产品数量",
   *       ),
   *       @OA\Property(
   *         property="point",
   *         type="integer",
   *         description="我的积分",
   *       ),
   *       @OA\Property(
   *         property="rz",
   *         type="integer",
   *         description="入驻状态 0 未申请 1 审核中 2 入驻成功 3 审核未通过",
   *       ),
   *       @OA\Property(
   *         property="rz_note",
   *         type="string",
   *         description="入驻审核未通过时原因",
   *       ),
   *       @OA\Property(
   *         property="tg",
   *         type="integer",
   *         description="推广申请状态 0 未申请 1 审核中 2 申请成功 3 审核未通过",
   *       ),
   *       @OA\Property(
   *         property="tg_note",
   *         type="string",
   *         description="推广审核未通过时原因",
   *       ),
   *       @OA\Property(
   *         property="ttg",
   *         type="integer",
   *         description="团队推广申请状态 0 未申请 1 审核中 2 申请成功 3 审核未通过",
   *       ),
   *       @OA\Property(
   *         property="ttg_note",
   *         type="string",
   *         description="团队推广审核未通过时原因",
   *       ),
   *       @OA\Property(
   *         property="ts_tel",
   *         type="string",
   *         description="投诉电话",
   *       ),
   *       @OA\Property(
   *         property="jm_tel",
   *         type="string",
   *         description="加盟热线",
   *       ),
   *       @OA\Property(
   *         property="hy",
   *         type="integer",
   *         description="会员状态 0 未申请 1 已申请",
   *       ),
   *       @OA\Property(
   *         property="upgrade",
   *         type="object",
   *         @OA\Property(property="status",type="integer",description="
   * 0 不可申请
   * 1 可申请
   * 2 审核中
   * 3 审核未通过"),
   *         @OA\Property(property="note",type="string",description="未通过原因"),
   *         description="身份升级",
   *       ),
   *       @OA\Property(
   *         property="counts",
   *         type="object",
   *         ref="#/components/schemas/UserCounts"
   *       ),
   *     )
   *   )
   * )
   */
  function load_user_index() {
    $uid = $this->getUid();
    $cid = '__login__' . $uid;
    $cache = $this->cacheGet($cid);
    $storeSettle = FALSE;
    $teamApply = FALSE;
    $errandApply = FALSE;
    if (!empty($cache)) {
      $data = $cache->data;
      //是否显示商家入驻
      $storeSettle = $data['storeSettle'];
      //显示内部推广申请入口
      $teamApply = $data['teamApply'];
      //跑腿申请
      $errandApply = $data['errandApply'];
    }

    $query = db_select('shusy_favorite_product', 'f');
    $query->fields('f', ['id']);
    $query->condition('uid', $uid);
    $query->condition('status', 1);
    $query->orderBy('id');
    $favorite_products = $query->execute()->rowCount();

    //入驻状态 0 未申请 1 审核中 2 入驻成功 3 审核未通过
    //0显示申请入驻 -1 不显示
    $rz = -1;
    $rz = 0;
    if ($storeSettle) {
      $rz = 0;
    }
    $rz_note = '';
    $es = shusy_store_load_multiple(FALSE, ['uid' => $uid]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyStore) {
        //状态 0 审核中 1 已通过 2 未通过
        switch ($e->status) {
          case 0:
            $rz = 1;
            break;
          case 1:
            $rz = 2;
            break;
          case 2:
            $rz = 3;
            $rz_note = $e->note;
            break;
        }
      }
    }

    //推广申请状态 0 未申请 1 审核中 2 申请成功 3 审核未通过
    [$tg, $tg_note] = $this->getTeamStatus($uid);

    $hy = 0;
    $es = shusy_vip_bind_load_multiple(FALSE, ['uid' => $uid]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyVipBind && $e->status == 1) {
        $hy = 1;
      }
    }


    //团队推广申请状态 0 未申请 1 审核中 2 申请成功 3 审核未通过
    //0显示申请入驻 -1 不显示
    $ttg = -1;
    if ($teamApply) {
      $ttg = 0;
    }
    $ttg_note = '';
    $es = shusy_team_apply_load_multiple(FALSE, ['uid' => $uid]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyTeamApply) {
        //状态 0 审核中 1 已通过 2 未通过
        switch ($e->status) {
          case 0:
            $ttg = 1;
            break;
          case 1:
            $ttg = 2;
            break;
          case 2:
            $ttg = 3;
            $ttg_note = $e->note;
            break;
        }
      }
    }
    $upgrade = ['status' => 0, 'node' => ''];
    $achievement_accumulative = points_get_total($uid, 'achievement_accumulative');
    $upgrade_company_1 = variable_get_value('shusy_team_achievement:upgrade:company_1') * 100;
    //升市场部长 需要单独申请
    if ($achievement_accumulative >= $upgrade_company_1) {
      $upgrade = ['status' => 1, 'node' => ''];
    }
    $es = shusy_team_company_apply_load_multiple(FALSE, ['uid' => $uid]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyTeamCompanyApply) {
        //状态 0 审核中 1 已通过 2 未通过
        switch ($e->status) {
          case 0:
            $upgrade = ['status' => 2, 'node' => ''];
            break;
          case 1:
            $upgrade = ['status' => 0, 'node' => ''];
            break;
          case 2:
            $upgrade = ['status' => 3, 'node' => $e->note];
            break;
        }
      }
    }

    [
      $type0,
      $type1,
      $type2,
      $type3,
      $type4,
      $type5,
    ] = $this->getMyOrderCounts();

    return [
      'code' => 0,
      'msg' => 'ok',
      'user' => User::get($uid),
      'favorite_products' => (int) $favorite_products,
      'point' => points_get_total($uid, 'point'),
      'rz' => $rz,
      'rz_note' => $rz_note,
      'tg' => $tg,
      'tg_note' => $tg_note,
      'ttg' => $ttg,
      'ttg_note' => $ttg_note,
      'ts_tel' => (string) variable_get_value('shushengyuan:settings:ts_tel'),
      'jm_tel' => (string) variable_get_value('shushengyuan:settings:jm_tel'),
      'hy' => $hy,
      'upgrade' => $upgrade,
      'errand' => $this->userErrand($errandApply),
      'counts' => [
        'type0' => $type0,
        'type1' => $type1,
        'type2' => $type2,
        'type3' => $type3,
        'type4' => $type4,
        'type5' => $type5,
        'errand' => $this->getErrandCounts(),
        'gift' => $this->getGiftCardCounts(),
        //未使用的服务订单数量
        'service' => $this->getServiceCounts(),
      ],
      'promote' => $this->getPromoteStatus($uid),
      'coupon' => $this->getMyCoupon($uid),
    ];
  }

  protected function getMyCoupon(int $uid) {
    $query = db_select('shusy_store_coupon_hold', 'h');
    $query->fields('h', ['id']);
    $query->condition('uid', $uid);
    $query->condition('status', 0);
    $dbAnd = db_and();
    $dbAnd->condition('validity_end', REQUEST_TIME, '>=');
    $query->condition($dbAnd);
    return $query->execute()->rowCount();
  }

  protected function getTeamStatus(int $uid) {
    $tg = -1;
    if (module_exists('shusy_promote')) {
      $es = shusy_promote_load_multiple(FALSE, ['uid' => $uid]);
      if (!empty($es)) {
        $e = reset($es);
        if ($e instanceof ShusyPromote) {
          if ($e->team == 1) {
            $tg = 0;
          }
        }
      }
    }
    //推广申请状态 0 未申请 1 审核中 2 申请成功 3 审核未通过
    $tg_note = '';
    if ($tg >= 0) {
      $es = shusy_distribution_load_multiple(FALSE, ['uid' => $uid]);
      if (!empty($es)) {
        $e = reset($es);
        if ($e instanceof ShusyDistribution) {
          //状态 0 审核中 1 已通过 2 未通过
          switch ($e->status) {
            case 0:
              $tg = 1;
              break;
            case 1:
              $tg = 2;
              break;
            case 2:
              $tg = 3;
              $tg_note = $e->note;
              break;
          }
        }
      }
    }
    return [$tg, $tg_note];
  }

  protected function getPromoteStatus(int $uid) {
    $status = 0;
    $note = '';
    if (module_exists('shusy_promote')) {
      $es = shusy_promote_load_multiple(FALSE, ['uid' => $uid]);
      if (!empty($es)) {
        $e = reset($es);
        if ($e instanceof ShusyPromote) {
          $status = $e->status;
          $note = $e->note;
        }
      }
    }
    return [
      //0 未申请 1 通过 2 不通过 3 审核中
      'status' => (int) $status,
      'note' => (string) $note,
    ];
  }

  /**
   * @return int
   */
  protected function getServiceCounts() {
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_service');
    $query->propertyCondition('uid', $uid);
    $query->propertyCondition('status', 0);
    $query->propertyOrderBy('id', 'DESC');
    return $query->count()->execute();
  }

  protected function userErrand(bool $errandApply) {
    $uid = $this->getUid();
    $errands = shusy_errand_load_multiple(FALSE, ['uid' => $uid]);
    if (!empty($errands)) {
      $errand = reset($errands);
      if ($errand instanceof ShusyErrand) {
        //返回 status 0 未申请 1 审核中 2 申请成功 3 审核未通过
        //状态 0 未提交 1 已通过 2 审核中 3 未通过
        switch ($errand->status) {
          case 0:
            return [
              'status' => 0,
              'note' => '',
            ];
          case 1:
            return [
              'status' => 2,
              'note' => '',
            ];
          case 2:
            return [
              'status' => 1,
              'note' => '',
            ];
          case 3:
            return [
              'status' => 3,
              'note' => '审核未通过',
            ];
        }
      }
    }
    if ($errandApply) {
      return [
        'status' => 0,
        'note' => '',
      ];
    }
    return [
      //-1不显示申请
      'status' => -1,
      'note' => '',
    ];
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/load_vip_rule",
   *   summary="会员等级规则 common/load_vip_rule",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="rule",
   *         type="string",
   *         description="会员等级规则",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="会员等级",
   *         @OA\Items(ref="#/components/schemas/VipItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_vip_rule() {
    $list = [];
    $es = shusy_vip_load_multiple(FALSE, ['status' => 1]);
    foreach ($es as $e) {
      if ($e instanceof ShusyVip) {
        $list[] = VipItem::get($e->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'rule' => variable_get_value('shusy_vip:rule')['value'],
      'list' => $list,
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_point_products",
   *   summary="获取积分兑换商品 common/load_point_products",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="积分产品列表",
   *         @OA\Items(ref="#/components/schemas/PointProductItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_point_products(int $offset, int $limit) {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_point_product');
    $query->propertyCondition('status', 1);
    $query->propertyOrderBy('sort');
    $query->propertyOrderBy('id', 'DESC');
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_point_product'] as $item) {
        $list[] = PointProductItem::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/load_point_product_info",
   *   summary="获取积分商品详情 common/load_point_product_info",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="PointProductItem.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="info",
   *         type="object",
   *         ref="#/components/schemas/PointProductInfo"
   *       ),
   *     )
   *   )
   * )
   */
  function load_point_product_info(int $id) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'info' => PointProductInfo::get($id),
    ];
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/payment_point_order",
   *   summary="结算积分兑换订单 common/payment_point_order",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="PointExchangeOrder.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="order",
   *         type="object",
   *         ref="#/components/schemas/PointExchangeOrder"
   *       ),
   *     )
   *   )
   * )
   */
  function payment_point_order(int $id) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'order' => PointExchangeOrder::get($id),
    ];
  }

  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_point_orders",
   *   summary="获取积分兑换订单 common/load_point_orders",
   *   security={{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="type",
   *     description="类型 0 待支付(包含部分支付的订单) 1 待收货 2 完成 3 取消",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="积分兑换订单列表",
   *         @OA\Items(ref="#/components/schemas/PointExchangeOrder")
   *       ),
   *     )
   *   )
   * )
   */
  function load_point_orders(int $type, int $offset, int $limit) {
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_point_product_order');
    $query->propertyCondition('uid', $uid);
    //状态 0 待支付 1 积分已支付 2 待发货 3 放弃订单 4 待收货 5 完成
    //类型 0 待支付(包含部分支付的订单) 1 待收货 2 完成 3 取消
    switch ($type) {
      case 0:
        $query->propertyCondition('status', [0, 1], 'IN');
        $query->propertyCondition('validity', REQUEST_TIME, '>=');
        break;
      case 1:
        $query->propertyCondition('status', [2, 4], 'IN');
        break;
      case 2:
        $query->propertyCondition('status', [5], 'IN');
        break;
      case 3:
        $query->propertyCondition('status', [3], 'IN');
        break;
      default:
        return [
          'code' => 1,
          'msg' => '状态不正确',
        ];
    }

    $query->propertyOrderBy('created', 'DESC');
    $query->range($offset, $limit);
    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_point_product_order'] as $item) {
        $list[] = PointExchangeOrder::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_point_list",
   *   summary="积分明细 common/load_point_list",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="积分明细",
   *         @OA\Items(ref="#/components/schemas/PointItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_point_list(int $offset, int $limit) {
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'points');
    $query->entityCondition('bundle', 'point');
    $query->propertyCondition('uid', $uid);
    $query->propertyOrderBy('id', 'desc');
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['points'] as $item) {
        $list[] = PointItem::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_my_services",
   *   summary="我购买的服务 common/load_my_services",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="type",
   *     description="类型 0 未使用 1 已使用 2 已过期 3 全部",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="我的服务",
   *         @OA\Items(ref="#/components/schemas/MyServiceItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_my_services(int $type, int $offset, int $limit) {
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_service');
    $query->propertyCondition('uid', $uid);
    //类型 0 未使用 1 已使用
    switch ($type) {
      case 0:
      case 1:
      case 2:
        $query->propertyCondition('status', $type);
        break;
      case 3:
        $query->propertyCondition('status', [0, 1, 2], 'IN');
        break;
      default:
        return [
          'code' => 1,
          'msg' => '类型错误',
        ];
    }
    $query->propertyOrderBy('validity', 'ASC');
    $query->propertyOrderBy('id', 'DESC');
    $query->range($offset, $limit);
    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_service'] as $item) {
        $list[] = MyServiceItem::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/load_my_service_info",
   *   summary="我的服务详情 common/load_my_service_info",
   *   security={{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="订单ID",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="info",
   *         type="object",
   *         ref="#/components/schemas/MyServiceInfo"
   *       ),
   *     )
   *   )
   * )
   */
  function load_my_service_info(int $id) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'info' => MyServiceInfo::get($id),
    ];
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/load_my_service_qr",
   *   summary="获取服务核销码 提供给商家扫码核销 5分钟有效 common/load_my_service_qr",
   *   security={{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="MyServiceItem.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="qr",
   *         type="string",
   *         description="二维码URL",
   *       ),
   *       @OA\Property(
   *         property="status",
   *         type="integer",
   *         description="状态 0 待使用 1 已使用 2 已过期",
   *       ),
   *       @OA\Property(
   *         property="statusLabel",
   *         type="string",
   *         description="状态标签",
   *       ),
   *     )
   *   )
   * )
   */
  function load_my_service_qr(int $id) {
    $uid = $this->getUid();
    $service = shusy_service_load($id);
    if ($service instanceof ShusyService && $service->uid == $uid) {
      $rand = mt_rand(1000, 9999);
      $cid = "{$id}{$uid}{$rand}";
      $this->cacheSet($cid, $id, REQUEST_TIME + (5 * 60));
      global $base_url;
      $statusLabel = '';
      switch ($service->status) {
        case 0:
          $statusLabel = '待使用';
          break;
        case 1:
          $statusLabel = '已使用';
          break;
        case 2:
          $statusLabel = '已过期';
          break;
      }
      return [
        'code' => 0,
        'msg' => 'ok',
        'qr' => $base_url . '/qr/show?data=' . $cid,
        'qrCode' => $cid,
        'status' => (int) $service->status,
        'statusLabel' => $statusLabel,
      ];
    }
    return [
      'code' => 1,
      'msg' => '服务不存在',
    ];
  }

  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_my_orders",
   *   summary="我的订单 common/load_my_orders",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="type",
   *     description="类型 0 全部 1 待付款 2 拼团中 3 待发货 4 待收货 5 待评价",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="我的订单",
   *         @OA\Items(ref="#/components/schemas/MyOrderItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_my_orders(int $type, int $offset, int $limit) {
    switch ($type) {
      case 2:
        $type = 10;
        break;
      case 3:
        $type = 2;
        break;
      case 4:
        $type = 3;
        break;
      case 5:
        $type = 4;
        break;
    }
    $uid = $this->getUid();
    $list = [];
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_store_order');
    $query->propertyCondition('uid', $uid);
    //状态 status 0 取消 1 购物车 2 待支付 3 待发货 4 待收货 5 待评价 6 已完成 7 未提交
    //类型 $type 0 全部 1 待付款 2 待发货 3 待收货 4 待评价
    switch ($type) {
      case 0:
        $query->propertyCondition('status', [0, 2, 3, 4, 5, 6, 10], 'IN');
        break;
      case 1:
        $query->propertyCondition('status', [2], 'IN');
        break;
      case 2:
        $query->propertyCondition('status', [3], 'IN');
        break;
      case 3:
        $query->propertyCondition('status', [4], 'IN');
        break;
      case 4:
        $query->propertyCondition('status', [5], 'IN');
        break;
      case 10:
        $query->propertyCondition('status', [10], 'IN');
        break;
      default:
        return [
          'code' => 1,
          'msg' => '类型不正确',
        ];
    }
    $query->propertyOrderBy('id', 'DESC');
    $query->range($offset, $limit);
    $result = $query->execute();
    if (!empty($result)) {
      foreach ($result['shusy_store_order'] as $item) {
        $list[] = MyOrderItem::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/loadMyOrderCount",
   *   summary="订单数量 common/loadMyOrderCount",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v3.0"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="type0",
   *         type="integer",
   *         description="type0数量",
   *       ),
   *       @OA\Property(
   *         property="type1",
   *         type="integer",
   *         description="type1数量",
   *       ),
   *       @OA\Property(
   *         property="type2",
   *         type="integer",
   *         description="type2数量",
   *       ),
   *       @OA\Property(
   *         property="type3",
   *         type="integer",
   *         description="type3数量",
   *       ),
   *       @OA\Property(
   *         property="type4",
   *         type="integer",
   *         description="type4数量",
   *       ),
   *       @OA\Property(
   *         property="type5",
   *         type="integer",
   *         description="type5数量",
   *       ),
   *     )
   *   )
   * )
   */
  function loadMyOrderCount() {
    [
      $type0,
      $type1,
      $type2,
      $type3,
      $type4,
      $type5,
    ] = $this->getMyOrderCounts();
    return [
      'code' => 0,
      'msg' => 'ok',
      'type0' => $type0,
      'type1' => $type1,
      'type2' => $type2,
      'type3' => $type3,
      'type4' => $type4,
      'type5' => $type5,
    ];
  }

  protected function getMyOrderCounts() {
    //类型 0 全部 1 待付款 2 拼团中 3 待发货 4 待收货 5 待评价
    $type0 = 0;
    $type1 = 0;
    $type2 = 0;
    $type3 = 0;
    $type4 = 0;
    $type5 = 0;
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_store_order');
    $query->propertyCondition('uid', $uid);
    //状态 status 0 取消 1 购物车 2 待支付 3 待发货 4 待收货 5 待评价 6 已完成 7 未提交
    //类型 $type 0 全部 1 待付款 2 待发货 3 待收货 4 待评价
    $types = [0, 1, 2, 3, 4, 5];
    foreach ($types as $type) {
      switch ($type) {
        case 2:
          $type = 10;
          break;
        case 3:
          $type = 2;
          break;
        case 4:
          $type = 3;
          break;
        case 5:
          $type = 4;
          break;
      }
      switch ($type) {
        case 0:
          $tmpQuery = clone $query;
          $tmpQuery->propertyCondition('status', [0, 2, 3, 4, 5, 6, 10], 'IN');
          $type0 = $tmpQuery->count()->execute();
          break;
        case 1:
          $tmpQuery = clone $query;
          $tmpQuery->propertyCondition('status', [2], 'IN');
          $type1 = $tmpQuery->count()->execute();
          break;
        case 2:
          $tmpQuery = clone $query;
          $tmpQuery->propertyCondition('status', [3], 'IN');
          $type3 = $tmpQuery->count()->execute();
          break;
        case 3:
          $tmpQuery = clone $query;
          $tmpQuery->propertyCondition('status', [4], 'IN');
          $type4 = $tmpQuery->count()->execute();
          break;
        case 4:
          $tmpQuery = clone $query;
          $tmpQuery->propertyCondition('status', [5], 'IN');
          $type5 = $tmpQuery->count()->execute();
          break;
        case 10:
          $tmpQuery = clone $query;
          $tmpQuery->propertyCondition('status', [10], 'IN');
          $type2 = $tmpQuery->count()->execute();
          break;
      }
    }
    return [$type0, $type1, $type2, $type3, $type4, $type5];
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/load_order_info",
   *   summary="订单详情 common/load_order_info",
   *   security={{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="订单ID",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="info",
   *         type="object",
   *         ref="#/components/schemas/OrderInfo"
   *       ),
   *     )
   *   )
   * )
   */
  function load_order_info(int $id) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'info' => OrderInfo::get($id),
    ];
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/load_errand_info",
   *   summary="获取快达信息 common/load_errand_info",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="rule",
   *         type="string",
   *         description="服务声明",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="区域负责人",
   *         @OA\Items(ref="#/components/schemas/ErrandItem")
   *       ),
   *       @OA\Property(
   *         property="isErrand",
   *         type="integer",
   *         description="是否是快达负责人 0 否 1 是",
   *       ),
   *     )
   *   )
   * )
   */
  function load_errand_info() {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_errand');
    $query->propertyCondition('status', 1);
    $query->propertyOrderBy('id');
    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_errand'] as $item) {
        $list[] = ErrandItem::get($item->id);
      }
    }

    $query = db_select('shusy_errand', 'l');
    $query->fields('l', ['id']);
    $query->condition('uid', $this->getUid());
    $query->condition('status', 1);
    $isErrand = $query->execute()->rowCount();

    return [
      'code' => 0,
      'msg' => 'ok',
      'rule' => variable_get_value('shusy_errand:rule'),
      'list' => $list,
      'isErrand' => (int) $isErrand,
    ];
  }

  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_errand_order",
   *   summary="获取快达订单 common/load_errand_order",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="type",
   *     description="类型 0 全部 1 待处理 2 配送中 3 已完成",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="快达订单",
   *         @OA\Items(ref="#/components/schemas/ErrandOrder")
   *       ),
   *     )
   *   )
   * )
   */
  function load_errand_order(int $type, int $offset, int $limit) {
    $uid = $this->getUid();
    $errand = $this->getErrand();
    if ($errand instanceof ShusyErrand) {
      $query = new EntityFieldQuery();
      $query->entityCondition('entity_type', 'shusy_errand_order');
      $query->propertyCondition('city_id', $errand->city_id);
      //需要检查当前人员的权限 是快达负责人 还是普通快达人员 普通快达人员仅查看分配给自己的订单
      if (!$errand->is_admin) {
        $query->propertyCondition('errand_id', $errand->id);
      }
      //类型 0 全部 1 待处理 2 配送中 3 已完成
      switch ($type) {
        case 0:
          break;
        case 1:
          $query->propertyCondition('status', [0], 'IN');
          break;
        case 2:
          $query->propertyCondition('status', [1], 'IN');
          break;
        case 3:
          $query->propertyCondition('status', [2], 'IN');
          break;
        default:
          return [
            'code' => 1,
            'msg' => '类型不正确',
          ];
      }
      $query->propertyOrderBy('id', 'DESC');
      $query->range($offset, $limit);

      $result = $query->execute();
      $list = [];
      $admin = (bool) $errand->is_admin;
      if (!empty($result)) {
        foreach ($result['shusy_errand_order'] as $item) {
          $list[] = ErrandOrder::get($item->id, FALSE, [$admin]);
        }
      }
      return [
        'code' => 0,
        'msg' => 'ok',
        'list' => $list,
      ];
    }
    return [
      'code' => 1,
      'msg' => '非快达人员',
    ];
  }

  protected function getErrandCounts() {
    $errand = $this->getErrand();
    if ($errand instanceof ShusyErrand) {
      $query = new EntityFieldQuery();
      $query->entityCondition('entity_type', 'shusy_errand_order');
      $query->propertyCondition('city_id', $errand->city_id);
      //需要检查当前人员的权限 是快达负责人 还是普通快达人员 普通快达人员仅查看分配给自己的订单
      if (!$errand->is_admin) {
        $query->propertyCondition('errand_id', $errand->id);
      }
      $query->propertyCondition('status', [0], 'IN');
      $query->propertyOrderBy('id', 'DESC');
      return $query->count()->execute();
    }
    return 0;
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/load_errand_order_info",
   *   summary="快达订单详情 common/load_errand_order_info",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="ErrandOrder.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="info",
   *         type="object",
   *         ref="#/components/schemas/ErrandOrderInfo"
   *       ),
   *     )
   *   )
   * )
   */
  function load_errand_order_info(int $id) {
    $errand = $this->getErrand();
    if ($errand instanceof ShusyErrand) {
      $order = shusy_errand_order_load($id);
      if ($order instanceof ShusyErrandOrder && $order->city_id == $errand->city_id) {
        return [
          'code' => 0,
          'msg' => 'ok',
          'info' => ErrandOrderInfo::get($id),
        ];
      }
    }
    return [
      'code' => 1,
      'msg' => '非快达人员',
    ];
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/load_expert_types",
   *   summary="专家咨询类型 common/load_expert_types",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="专家咨询类型",
   *         @OA\Items(ref="#/components/schemas/ExpertType")
   *       ),
   *     )
   *   )
   * )
   */
  function load_expert_types() {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_expert_type');
    $query->propertyCondition('status', 1);
    $query->propertyOrderBy('sort', 'ASC');
    $query->propertyOrderBy('id');
    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_expert_type'] as $item) {
        $list[] = ExpertType::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_experts",
   *   summary="专家咨询列表 common/load_experts",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="type",
   *     description="ExpertType.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="专家咨询",
   *         @OA\Items(ref="#/components/schemas/ExpertItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_experts(int $type, int $offset, int $limit) {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_expert');
    $query->propertyCondition('expert_type_id', $type);
    $query->propertyCondition('status', 1);
    $query->propertyOrderBy('created');
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_expert'] as $item) {
        $list[] = ExpertItem::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/load_expert_info",
   *   summary="专家咨询详情 common/load_expert_info",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="ExpertItem.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="info",
   *         type="object",
   *         ref="#/components/schemas/ExpertInfo"
   *       ),
   *     )
   *   )
   * )
   */
  function load_expert_info(int $id) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'info' => ExpertInfo::get($id),
    ];
  }

  /**
   * @param string $in_month
   * @return array
   * @OA\Get(
   *   path="/common/load_sign_list",
   *   summary="获取本月签到信息 common/load_sign_list",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="in_month",
   *     description="需要查看的月份信息 YYYY-MM-DD 格式 不传显示当前月",
   *     @OA\Schema(
   *       type="string",
   *     ),
   *     in="query",
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="today",
   *         type="integer",
   *         description="今日是否签到 0 未签到 1 已签到",
   *       ),
   *       @OA\Property(
   *         property="total_sign",
   *         type="integer",
   *         description="已连续签到天数",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="签到信息列表",
   *         @OA\Items(ref="#/components/schemas/SignItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_sign_list(string $in_month = NULL) {
    if (empty($in_month)) {
      $in_month = format_date(REQUEST_TIME, 'custom', 'Y-m-d');
    }
    $start = strtotime("first day of $in_month 00:00");
    $end = strtotime("first day of +1 month $in_month 00:00");
    $uid = $this->getUid();

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_sign');
    $query->propertyCondition('uid', $uid);
    $query->propertyCondition('created', $start, '>=');
    $query->propertyCondition('created', $end, '<');
    $query->propertyOrderBy('created');

    $result = $query->execute();
    //占位类型 0 空白占位 1 正常数据 2 日期占位
    $signs = [];
    if (!empty($result)) {
      foreach ($result['shusy_sign'] as $item) {
        $entity = shusy_sign_load($item->id);
        if ($entity instanceof ShusySign) {
          $day = format_date($entity->created, 'custom', 'Ymd');
          $signs[$day] = SignItem::init(FALSE, FALSE, [1, $entity->created]);
        }
      }
    }

    $list = [];

    /**
     * N  ISO-8601 格式数字表示的星期中的第几天（PHP 5.1.0 新加）  1（表示星期一）到 7（表示星期天）
     * w  星期中的第几天，数字表示  0（表示星期天）到 6（表示星期六）
     */
    //计算前置补充天数
    $inWeek = format_date($start, 'custom', 'N');
    if (($inWeek - 1) > 0) {
      for ($i = ($inWeek - 1); $i <= 7; $i++) {
        $list[] = SignItem::get(FALSE, FALSE, [0, 0]);
      }
    }

    $total_sign = 0;
    for ($i = $start; $i < $end; $i = $i + 24 * 60 * 60) {
      $iDay = format_date($i, 'custom', 'Ymd');
      if (isset($signs[$iDay])) {
        $list[] = $signs[$iDay];
        $total_sign++;
      }
      else {
        if ($i <= REQUEST_TIME) {
          $total_sign = 0;
        }
        $list[] = SignItem::get(FALSE, FALSE, [2, $i]);
      }
    }

    $in_day = format_date(REQUEST_TIME, 'custom', 'Ymd');
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_sign');
    $query->propertyCondition('uid', $uid);
    $query->propertyCondition('in_day', $in_day);
    $query->propertyOrderBy('id');
    $query->range(0, 1);
    $result = $query->count()->execute();

    return [
      'code' => 0,
      'msg' => 'ok',
      'today' => (int) $result,
      'total_sign' => (int) $total_sign,
      'list' => $list,
    ];
  }

  function loadSignIndex() {
    $in_month = format_date(REQUEST_TIME, 'custom', 'Y-m-d');
    $start = strtotime("first day of $in_month 00:00");
    $end = strtotime("first day of +1 month $in_month 00:00");
    $uid = $this->getUid();

    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_sign');
    $query->propertyCondition('uid', $uid);
    $query->propertyCondition('created', $start, '>=');
    $query->propertyCondition('created', $end, '<');
    $query->propertyOrderBy('created');

    $result = $query->execute();
    //占位类型 0 空白占位 1 正常数据 2 日期占位
    $signs = [];
    if (!empty($result)) {
      foreach ($result['shusy_sign'] as $item) {
        $entity = shusy_sign_load($item->id);
        if ($entity instanceof ShusySign) {
          $day = format_date($entity->created, 'custom', 'Ymd');
          $signs[$day] = SignItem::init(FALSE, FALSE, [1, $entity->created]);
        }
      }
    }


    $total_sign = 0;
    for ($i = $start; $i < $end; $i = $i + 24 * 60 * 60) {
      $iDay = format_date($i, 'custom', 'Ymd');
      if (isset($signs[$iDay])) {
        $total_sign++;
      }
      else {
        if ($i <= REQUEST_TIME) {
          $total_sign = 0;
        }
      }
    }

    $signruleid = variable_get_value('shushengyuan:article:signrule:id');

    return [
      'code' => 0,
      'msg' => 'ok',
      'point' => [
        'point' => points_get_total($uid, 'point'),
        'ad' => 'https://shushengyuan.sccxbe.com/imgs/p-ad.png',
      ],
      'calendar' => CalendarWrapper::init(FALSE, FALSE, [$uid]),
      'series' => [
        's100' => $total_sign > 99 ? substr($total_sign, -3, 1) ?? 0 : 0,
        's10' => $total_sign > 9 ? substr($total_sign, -2, 1) ?? 0 : 0,
        's1' => substr($total_sign, -1, 1) ?? 0,
      ],
      'products' => $this->load_point_products(0, 50)['list'],
      'rule' => "/pages/article/article?id={$signruleid}",
    ];
  }

  /**
   * @param int $type
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/load_text",
   *   summary="获取文字信息 common/load_text",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="type",
   *     description="类型 0 关于我们 1 平台声明 2 公告详情 4 快达协议 5 用户协议",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="id",
   *     description="公告ID",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="body",
   *         type="string",
   *         description="富文本",
   *       ),
   *     )
   *   )
   * )
   */
  function load_text(int $type, $id = 0) {
    switch ($type) {
      case 0:
      case 1:
      case 4:
      case 5:
        $info = variable_get_value("shushengyuan:text:{$type}:body");
        return [
          'code' => 0,
          'msg' => 'ok',
          'body' => $info['value'],
        ];
        break;
      case 2:
        $tmp = AnnouncementItem::get($id);
        return [
          'code' => 0,
          'msg' => 'ok',
          'body' => $tmp['body'],
        ];
        break;
    }
    return [
      'code' => 1,
      'msg' => '未找到相关配置',
    ];
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/load_distribution_info",
   *   summary="我的推广 common/load_distribution_info",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="user",
   *         type="object",
   *         ref="#/components/schemas/User"
   *       ),
   *       @OA\Property(
   *         property="parent",
   *         type="object",
   *         description="上家",
   *         ref="#/components/schemas/User"
   *       ),
   *       @OA\Property(
   *         property="created",
   *         type="string",
   *         description="推广时间",
   *       ),
   *       @OA\Property(
   *         property="accumulative",
   *         type="number",
   *         description="累计收益",
   *       ),
   *       @OA\Property(
   *         property="locked",
   *         type="number",
   *         description="锁定中",
   *       ),
   *       @OA\Property(
   *         property="income",
   *         type="number",
   *         description="可提现",
   *       ),
   *     )
   *   )
   * )
   */
  function load_distribution_info() {
    $uid = $this->getUid();
    $user = user_load($uid);
    $d = Distribution::getDistribution($uid);
    $nickname = '无';
    $created = $this->formatDate($user->created, 'Y-m-d');
    if ($d instanceof Distribution) {
      $pid = $d->pid;
      if ($pid > 0) {
        $parent = User::get($pid);
        if (!empty($parent)) {
          if (isset($parent['nickname'])) {
            $nickname = $parent['nickname'];
          }
        }
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'user' => User::get($uid),
      'parent' => ['nickname' => $nickname],
      'created' => $created,
      'accumulative' => $this->convertYuan(points_get_total($uid, 'accumulative')),
      'locked' => $this->convertYuan(points_get_total($uid, 'locked')),
      'income' => $this->convertYuan(points_get_total($uid, 'income')),
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_cash_list",
   *   summary="提现明细 common/load_cash_list",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="提现明细",
   *         @OA\Items(ref="#/components/schemas/CashItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_cash_list(int $offset, int $limit) {
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_cash');
    $query->propertyCondition('uid', $uid);
    $query->propertyOrderBy('id', 'DESC');
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_cash'] as $item) {
        $list[] = CashItem::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_income_order",
   *   summary="推广订单 common/load_income_order",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="type",
   *     description="类型 0 全部 1 为结算 2 已结算",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="推广订单",
   *         @OA\Items(ref="#/components/schemas/IncomeOrderItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_income_order(int $type, int $offset, int $limit) {
    $uid = $this->getUid();
    $query = db_select('youji_income_order', 'o');
    $query->innerJoin('shusy_store_order', 'so', 'so.id=o.order_id');
    $query->innerJoin('users', 'u', 'u.uid=o.uid');
    $query->fields('o', ['id']);
    $query->orderBy('id', 'desc');
    $query->range($offset, $limit);
    $query->condition('o.uid', $uid);
    switch ($type) {
      case 0:
        $query->condition('o.status', [0, 1], 'IN');
        break;
      case 1:
        $query->condition('o.status', [0], 'IN');
        break;
      case 2:
        $query->condition('o.status', [1], 'IN');
        break;
      default:
        return [
          'code' => 1,
          'msg' => '类型不正确',
        ];
    }
    $result = $query->execute();
    $list = [];
    while ($row = $result->fetchAssoc()) {
      $list[] = IncomeOrderItem::get($row['id'], FALSE);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_my_teams",
   *   summary="我的团队 common/load_my_teams",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="type",
   *     description="类型 0 一级团队 1 二级团队",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="我的团队",
   *         @OA\Items(ref="#/components/schemas/MyTeamItem")
   *       ),
   *       @OA\Property(
   *         property="t1",
   *         type="integer",
   *         description="一级团队人数",
   *       ),
   *       @OA\Property(
   *         property="t2",
   *         type="integer",
   *         description="二级团队人数",
   *       ),
   *     )
   *   )
   * )
   */
  function load_my_teams(int $type, int $offset, int $limit) {
    $list = [];
    $uid = $this->getUid();
    // $uid = 324;
    // $uid = 1037;
    $cid = '__login__' . $uid;
    [$tg, $tg_note] = $this->getTeamStatus($uid);

    $query = db_select('distribution', 'd');
    $query->fields('d', ['id']);
    $query->condition('pid', $uid);
    $query->orderBy('id', 'DESC');
    $t1Query = clone $query;
    $t1 = $t1Query->execute()->rowCount();

    $query = db_select('distribution', 'd');
    $query->leftJoin('distribution', 'd2', 'd2.uid=d.pid');
    $query->fields('d', ['id']);
    $query->distinct();
    $query->condition('d.pid', $uid,"<>");
    if($tg==2){
      $query->condition(db_or()->condition('d2.pid',$uid)->condition('d.tid',$uid)->condition('d2.tid',$uid));
    }else{
      $query->condition('d2.pid', $uid);
    }
    $query->orderBy('d.id', 'DESC');
    $t2Query = clone $query;
    $t2 = $t2Query->execute()->rowCount();

    $query = db_select('distribution', 'd');
    $query->leftJoin('distribution', 'd2', 'd2.uid=d.pid');
    $query->fields('d', ['id']);
    $query->distinct();
    if($tg==2){
      $query->condition(db_or()->condition('d.pid',$uid)->condition('d2.pid',$uid)->condition('d.tid',$uid));
    }else{
      $query->condition(db_or()->condition('d.pid',$uid)->condition('d2.pid',$uid));
    }
    $query->orderBy('d.id', 'DESC');
    $t1Query = clone $query;
    $t3 = $t1Query->execute()->rowCount();

    switch ($type) {
      case 0:
        $query = db_select('distribution', 'd');
        $query->fields('d', ['id']);
        $query->condition('pid', $uid);
        $query->orderBy('id', 'DESC');
        $query->range($offset, $limit);
        $result = $query->execute();
        while ($row = $result->fetchAssoc()) {
          $list[] = MyTeamItem::get($row['id']);
        }
        break;
      case 1:
        $query = db_select('distribution', 'd');
        $query->leftJoin('distribution', 'd2', 'd2.uid=d.pid');
        $query->fields('d', ['id']);
        $query->condition('d.pid', $uid,"<>");
        if($tg==2){
          $query->condition(db_or()->condition('d2.pid',$uid)->condition('d.tid',$uid)->condition('d.tid',$uid));
        }else{
          $query->condition('d2.pid', $uid);
        }
        $query->distinct();
        $query->orderBy('d.id', 'DESC');
        $query->range($offset, $limit);
        $result = $query->execute();
        while ($row = $result->fetchAssoc()) {
          $list[] = MyTeamItem::get($row['id']);
        }
        break;
      case 2:
        // $query = db_select('distribution', 'd');
        // $query->fields('d', ['id']);
        // $query->condition('tid', $uid);
        // $query->orderBy('id', 'DESC');
        // $query->range($offset, $limit);
        // $result = $query->execute();
        // while ($row = $result->fetchAssoc()) {
        //   $list[] = MyTeamItem::get($row['id']);
        // }
        $query = db_select('distribution', 'd');
        $query->leftJoin('distribution', 'd2', 'd2.uid=d.pid');
        $query->fields('d', ['id']);
        $query->distinct();
        if($tg==2){
          $query->condition(db_or()->condition('d.pid',$uid)->condition('d2.pid',$uid)->condition('d2.tid',$uid));
        }else{
          $query->condition(db_or()->condition('d.pid',$uid)->condition('d2.pid',$uid));
        }
        $query->orderBy('d.id', 'DESC');
        $query->range($offset, $limit);
        $result = $query->execute();
        while ($row = $result->fetchAssoc()) {
          $list[] = MyTeamItem::get($row['id']);
        }
        break;
      default:
        return [
          'code' => 1,
          'msg' => '类型不正确',
        ];
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
      't1' => $t1,
      't2' => $t2,
      't3' => $t3,
      'team' => $this->getIsTeam($uid),
    ];
  }
  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_my_taxation",
   *   summary="我的团队 common/load_my_taxation",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="type",
   *     description="类型 0 一级团队 1 二级团队",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="我的团队",
   *         @OA\Items(ref="#/components/schemas/MyTeamItem")
   *       )
   *     )
   *   )
   * )
   */
  function load_my_taxation(int $type,int $uid=0, int $offset, int $limit) {
    $list = [];
    if($uid==0){
      $uid = $this->getUid();
    }
    // var_dump($uid);
    $query = db_select('shusy_taxation', 'd');
    $query->fields('d', ['id']);
    if($type>0){
      $query->condition('status', $type);
    }

    $query->condition('uid', $uid);
    $query->orderBy('id', 'DESC');
    $query->range($offset, $limit);
    $result = $query->execute();
    while ($row = $result->fetchAssoc()) {
      $list[] = MyTaxationItem::get($row['id']);
    }

    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
      't1' => 0,
      't2' => 0,
      't3' =>0,
      'team' => '',
    ];
  }
  function load_my_taxationinfo(int $id) {
    $list = [];
    $list = MyTaxationItem::get($id);

    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
      't1' => 0,
      't2' => 0,
      't3' =>0,
      'team' => '',
    ];
  }
  protected function getIsTeam(int $uid) {
    $es = shusy_distribution_load_multiple(FALSE, ['uid' => $uid]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyDistribution) {
        if ($e->status == 1) {
          return TRUE;
        }
      }
    }
    return FALSE;
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/load_share_qr",
   *   summary="获取推广码 common/load_share_qr",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="qr",
   *         type="string",
   *         description="推广码url",
   *       ),
   *     )
   *   )
   * )
   */
  function load_share_qr() {
    $uid = $this->getUid();
    $uri = $this->getShareQr($uid);
    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'qr' => file_create_url($uri),
    ];
  }

  protected function getShareQr(int $uid) {
    $uri = wx_mini_qr_create('shushengyuan', 'share_qr_' . $uid, [
      'uid' => $uid,
      'type' => 'share',
    ]);
    return $uri;
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/loadTeamQr",
   *   summary="团队推广码获取 common/loadTeamQr",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v2.2"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="team",
   *         type="string",
   *         description="推广团队成员码url",
   *       ),
   *       @OA\Property(
   *         property="store",
   *         type="string",
   *         description="推广店铺码url",
   *       ),
   *     )
   *   )
   * )
   */
  function loadTeamQr() {
    $uid = $this->getUid();
    $es = shusy_team_apply_load_multiple(FALSE, ['uid' => $uid]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyTeamApply) {
        switch ($e->status) {
          case 1:
            return [
              'code' => 0,
              'msg' => 'ok',
              'team' => file_create_url($this->getTeamQrTeam($uid)),
              'store' => file_create_url($this->getTeamQrStore($uid)),
            ];
        }
        return [
          'code' => 1,
          'msg' => '申请审核中或未通过',
        ];
      }
    }
    return [
      'code' => 1,
      'msg' => '未申请为团队推广',
    ];
  }

  protected function getTeamQrTeam(int $uid) {
    return wx_mini_qr_create('shusy_team', 'teamApply_' . $uid, [
      'uid' => $uid,
      'type' => 'team',
    ]);
  }

  protected function getErrandQr(int $uid) {
    return wx_mini_qr_create('shusy_errand', 'errandApply_' . $uid, [
      'uid' => $uid,
      'type' => 'errand',
    ]);
  }

  protected function getTeamQrStore(int $uid) {
    return wx_mini_qr_create('shusy_team', 'storeApply_' . $uid, [
      'uid' => $uid,
      'type' => 'team',
    ]);
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/loadOrderLogistics",
   *   summary="物流查询 common/loadOrderLogistics",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v2.2"},
   *   @OA\Parameter(
   *     name="id",
   *     description="订单ID",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="order",
   *         type="object",
   *         ref="#/components/schemas/OrderLogistics"
   *       ),
   *     )
   *   )
   * )
   */
  function loadOrderLogistics(int $id) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'order' => OrderLogistics::init($id),
    ];
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/load_store_center",
   *   summary="商户中心 common/load_store_center",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0","store"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="store",
   *         type="object",
   *         description="店铺信息",
   *         ref="#/components/schemas/StoreItem"
   *       ),
   *     )
   *   )
   * )
   * @throws \ServicesException
   */
  function load_store_center() {
    $store = $this->getStore();
    if ($store instanceof ShusyStore) {
      return [
        'code' => (int) 0,
        'msg' => (string) 'ok',
        'store' => StoreItem::get($store->id),
      ];
    }
    return [
      'code' => 1,
      'msg' => '店铺不存在',
    ];
  }

  /**
   * @return array
   * @throws \ServicesException
   * @OA\Get(
   *   path="/common/load_store_qr",
   *   summary="店铺二维码 common/load_store_qr",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0","store"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="qr",
   *         type="string",
   *         description="店铺码url",
   *       ),
   *     )
   *   )
   * )
   */
  function load_store_qr() {
    $uid = $this->getUid();
    $store = $this->getStore();
    if ($store instanceof ShusyStore) {
      $uri = $this->getStoreQr($uid, $store);
      return [
        'code' => (int) 0,
        'msg' => (string) 'ok',
        'qr' => file_create_url($uri),
      ];
    }
    return [
      'code' => 1,
      'msg' => '店铺不存在',
    ];
  }

  protected function getStoreQr(int $uid, ShusyStore $store) {
    $uri = wx_mini_qr_create('shushengyuan', 'share_qr_' . $uid, [
      'uid' => $uid,
      'store' => $store->id,
      'type' => 'store',
    ]);
    return $uri;
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @throws \SearchApiException
   * @throws \ServicesException
   * @OA\Get(
   *   path="/common/load_my_store_products",
   *   summary="我店铺的商品 common/load_my_store_products",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0","store"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         @OA\Items(ref="#/components/schemas/MyStoreProductItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_my_store_products(int $offset, int $limit) {
    $store = $this->getStore();
    if ($store instanceof ShusyStore && $store->status == 1) {
      $keys = '';
      $sort = [];
      $filter = [
        '~and' => [
          'store:id' => $store->id,
        ],
      ];
      $result = $this->_searchApiResourceRetrieve('products', $keys, $filter, $sort, $limit, $offset);
      $list = [];
      foreach ($result['results'] as $id => $item) {
        $list[] = MyStoreProductItem::get($id);
      }
      $facets = [];
      foreach ($result['facets'] as $facet) {
        if (!empty($facet['items'])) {
          $facets[] = [
            'items' => $facet['items'],
            'title' => $facet['title'],
          ];
        }
      }
      return [
        'code' => (int) 0,
        'msg' => (string) 'ok',
        'list' => $list,
        'count' => (int) $result['result count'],
        'facets' => $facets,
        'sorts' => $result['sorts'],
      ];
    }
    return [
      'code' => 1,
      'msg' => '店铺不存在或审核中',
    ];
  }

  /**
   * @return array
   * @throws \ServicesException
   * @OA\Get(
   *   path="/common/load_my_store_performance_total",
   *   summary="查看店铺总业绩 common/load_my_store_performance_total",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0","store"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="performance",
   *         type="number",
   *         description="总业绩",
   *       ),
   *     )
   *   )
   * )
   */
  function load_my_store_performance_total() {
    $store = $this->getStore();
    if ($store instanceof ShusyStore) {
      $income = points_get_total($store->uid, 'income');
      return [
        'code' => 0,
        'msg' => 'ok',
        'performance' => $this->convertYuan($income),
      ];
    }
    return [
      'code' => 1,
      'msg' => '店铺不存在或审核中',
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @param string $in_month
   * @return array
   * @throws \ServicesException
   * @OA\Get(
   *   path="/common/load_my_store_performance",
   *   summary="查看店铺业绩明细 common/load_my_store_performance",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0","store"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="in_month",
   *     description="需要查看的月 YYYY-MM-DD 格式 不传查看当前月",
   *     @OA\Schema(
   *       type="string",
   *       default=""
   *     ),
   *     in="query",
   *     required=false,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         @OA\Items(ref="#/components/schemas/MyStorePerformanceItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_my_store_performance(int $offset, int $limit, string $in_month = NULL) {
    $store = $this->getStore();
    if ($store instanceof ShusyStore) {
      if (empty($in_month)) {
        $in_month = format_date(REQUEST_TIME, 'custom', 'Y-m-d');
      }
      $start = strtotime("first day of $in_month 00:00");
      $end = strtotime("first day of +1 month $in_month 00:00");

      $query = new EntityFieldQuery();
      $query->entityCondition('entity_type', 'shusy_store_performance');
      $query->propertyCondition('store_id', $store->id);
      /**
       * locked
       * income
       */
      $query->propertyCondition('type', 'income');
      $query->propertyCondition('created', $start, '>=');
      $query->propertyCondition('created', $end, '<');
      $query->propertyOrderBy('id', 'DESC');
      $query->range($offset, $limit);

      $result = $query->execute();
      $list = [];
      if (!empty($result)) {
        foreach ($result['shusy_store_performance'] as $item) {
          $list[] = MyStorePerformanceItem::get($item->id);
        }
      }
      return [
        'code' => 0,
        'msg' => 'ok',
        'list' => $list,
      ];
    }
    return [
      'code' => 1,
      'msg' => '店铺不存在或审核中',
    ];
  }

  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * @throws \ServicesException
   * @OA\Get(
   *   path="/common/load_my_store_order",
   *   summary="店铺订单 common/load_my_store_order",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.0","store"},
   *   @OA\Parameter(
   *     name="type",
   *     description="类型 0 全部订单 1 待发货 2 待收货 3 待评价 4 已完成",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         @OA\Items(ref="#/components/schemas/MyStoreOrderItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_my_store_order(int $type, int $offset, int $limit) {
    $list = [];
    $store = $this->getStore();
    if ($store instanceof ShusyStore) {
      $query = new EntityFieldQuery();
      $query->entityCondition('entity_type', 'shusy_store_order');
      $query->propertyCondition('store_id', $store->id);
      //状态 status 0 取消 1 购物车 2 待支付 3 待发货 4 待收货 5 待评价 6 已完成 7 未提交
      //类型 $type 0 全部订单 1 待发货 2 待收货 3 待评价 4 已完成
      switch ($type) {
        case 0:
          $query->propertyCondition('status', [3, 4, 5, 6], 'IN');
          break;
        case 1:
          $query->propertyCondition('status', [3], 'IN');
          break;
        case 2:
          $query->propertyCondition('status', [4], 'IN');
          break;
        case 3:
          $query->propertyCondition('status', [5], 'IN');
          break;
        case 4:
          $query->propertyCondition('status', [6], 'IN');
          break;
        default:
          return [
            'code' => 1,
            'msg' => '类型不正确',
          ];
      }
      $query->propertyOrderBy('id', 'DESC');
      $query->range($offset, $limit);
      $result = $query->execute();
      if (!empty($result)) {
        foreach ($result['shusy_store_order'] as $item) {
          $list[] = MyStoreOrderItem::get($item->id);
        }
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * @throws \ServicesException
   * @OA\Get(
   *   path="/common/load_my_store_service_order",
   *   summary="店铺服务订单 common/load_my_store_service_order",
   *   security={{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v1.0","store"},
   *   @OA\Parameter(
   *     name="type",
   *     description="类型 0 全部 1 待使用 2 已使用 3 已过期",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         @OA\Items(ref="#/components/schemas/MyStoreServiceOrderItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_my_store_service_order(int $type, int $offset, int $limit) {
    $store = $this->getStore();
    if ($store instanceof ShusyStore) {
      $query = new EntityFieldQuery();
      $query->entityCondition('entity_type', 'shusy_service');
      $query->propertyCondition('store_id', $store->id);
      //类型 0 未使用 1 已使用
      switch ($type) {
        case 0:
          $query->propertyCondition('status', [0, 1, 2], 'IN');
          break;
        case 1:
        case 2:
        case 3:
          $query->propertyCondition('status', $type - 1);
          break;
        default:
          return [
            'code' => 1,
            'msg' => '类型错误',
          ];
      }
      $query->propertyOrderBy('validity', 'ASC');
      $query->propertyOrderBy('id', 'DESC');
      $query->range($offset, $limit);
      $result = $query->execute();
      $list = [];
      if (!empty($result)) {
        foreach ($result['shusy_service'] as $item) {
          $list[] = MyStoreServiceOrderItem::get($item->id);
        }
      }
      return [
        'code' => 0,
        'msg' => 'ok',
        'list' => $list,
      ];
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => [],
    ];
  }

  /**
   * @param int $type
   * @return array
   * @OA\Get(
   *   path="/common/load_bgimg",
   *   summary="获取背景图 common/load_bgimg",
   *   security={{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Parameter(
   *     name="type",
   *     description="类型 0 推广申请 1 商户申请 2 VIP申请",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="image",
   *         type="string",
   *         description="背景图URL",
   *       ),
   *     )
   *   )
   * )
   */
  function load_bgimg(int $type) {
    $imgs = shushengyuan_bgimgs();
    if (isset($imgs[$type])) {
      $uri = variable_get_value('shushengyuan:bgimg:' . $type);
      return [
        'code' => 0,
        'msg' => 'ok',
        'image' => $this->imageStyleUrl('bgimg', $uri),
      ];
    }
    return [
      'code' => 1,
      'msg' => '背景图未定义',
    ];
  }


  /**
   * @param int $id
   * @return array
   * @throws \ServicesException
   * @OA\Get(
   *   path="/common/load_order_service_info",
   *   summary="获取售后单详情 common/load_order_service_info",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.1","vStore"},
   *   @OA\Parameter(
   *     name="id",
   *     description="售后单ID",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="service",
   *         type="object",
   *         description="售后单详情",
   *         ref="#/components/schemas/OrderServiceInfo"
   *       ),
   *     )
   *   )
   * )
   */
  function load_order_service_info(int $id) {
    $uid = $this->getUid();
    $service = s_commerce_service_load($id);
    if ($service instanceof SCommerceService && $service->uid == $uid) {
      return [
        'code' => 0,
        'msg' => 'ok',
        'service' => OrderServiceInfo::get($id),
      ];
    }
    $store = $this->getStore();
    if ($store instanceof ShusyStore && $service->store_id == $store->id) {
      return [
        'code' => 0,
        'msg' => 'ok',
        'service' => OrderServiceInfo::get($id),
      ];
    }
    return [
      'code' => 1,
      'msg' => '售后单不存在',
    ];
  }

  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/load_order_services",
   *   summary="获取售后订单 common/load_order_services",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.1"},
   *   @OA\Parameter(
   *     name="type",
   *     description="状态 0 全部 1 已申请 2 退款中 3 已完成",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="售后订单",
   *         @OA\Items(ref="#/components/schemas/OrderServiceItem")
   *       ),
   *     )
   *   )
   * )
   * 获取售后单列表
   */
  function load_order_services(int $type, int $offset, int $limit) {
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 's_commerce_service');
    $query->propertyCondition('uid', $uid);
    //状态 0 全部 1 已申请 2 退款中 3 已完成
    switch ($type) {
      case 0:
        $query->propertyCondition('status', [0, 1, 2, 3, 4], 'IN');
        break;
      case 1:
        $query->propertyCondition('status', [0, 4], 'IN');
        break;
      case 2:
        $query->propertyCondition('status', [1], 'IN');
        break;
      case 3:
        $query->propertyCondition('status', [2, 3], 'IN');
        break;
      default:
        return [
          'code' => 1,
          'msg' => '请选择正确的状态',
        ];
    }
    $query->propertyOrderBy('created', 'DESC');
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['s_commerce_service'] as $item) {
        $list[] = OrderServiceItem::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @return array
   * 退款原因
   * @OA\Get(
   *   path="/common/load_service_reason",
   *   summary="获取退款原因列表 common/load_service_reason",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.1"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="退款原因列表",
   *         @OA\Items(ref="#/components/schemas/ServiceReasonItem")
   *       ),
   *     )
   *   )
   * )
   */
  function load_service_reason() {
    $vocabulary = taxonomy_vocabulary_machine_name_load('reason');
    $terms = taxonomy_term_load_multiple(FALSE, ['vid' => $vocabulary->vid]);
    $options = [];
    $terms_wrapper = entity_metadata_wrapper('list<taxonomy_term>', $terms);
    if ($terms_wrapper instanceof EntityListWrapper) {
      foreach ($terms_wrapper->getIterator() as $item) {
        $options[] = [
          'id' => (int) $item->tid->value(),
          'name' => $item->name->value(),
          'weight' => $item->weight->value(),
        ];
      }
    }
    usort($options, 'drupal_sort_weight');
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => array_values($options),
    ];
  }

  function loadServiceLineItem(int $id) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'product' => LineItemProduct::init($id),
    ];
  }

  /**
   * @param int $id Order.id
   * @param int $lid
   * @return array
   * @OA\Get(
   *   path="/common/load_order_refund_amount",
   *   summary="获取订单项可以退款的总金额 common/load_order_refund_amount",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.1"},
   *   @OA\Parameter(
   *     name="id",
   *     description="OrderCart.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="lid",
   *     description="LineItemProduct.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="amount",
   *         type="number",
   *         description="可退款总金额",
   *       ),
   *     )
   *   )
   * )
   * 计算订单项可以退的费用
   */
  function load_order_refund_amount(int $id, int $lid) {
    $order = shusy_store_order_load($id);
    $line = shusy_store_order_line_load($lid);
    $uid = $this->getUid();
    if ($order instanceof ShusyStoreOrder && $line instanceof ShusyStoreOrderLine && $line->store_order_id == $order->id && $order->uid == $uid) {
      //计算已退款金额
      $refund = 0;
      $es = entity_load('s_commerce_service', FALSE, [
        'line_item_id' => $lid,
      ]);
      foreach ($es as $e) {
        if ($e instanceof SCommerceService) {
          switch ($e->status) {
            //状态 0 待审核 1 待退款 2 已完成 3 拒绝 4 待退货
            case 0:
              break;
            case 1:
              break;
            case 2:
              $refund += $e->amount;
              break;
            case 3:
              break;
          }
        }
      }

      $lastOne = TRUE;
      $lines = $order->lines();
      foreach ($lines as $item) {
        //需要检查是否是最后一个
        if ($lastOne == TRUE && $item instanceof ShusyStoreOrderLine) {
          try {
            if ($item->type == 'product') {
              //检查项售后状态 是否为最后一个
              $line_item_id = $item->id;
              if ($line_item_id != $lid) {
                $scses = s_commerce_service_line_load_multiple(FALSE, ['line_item_id' => $line_item_id]);
                if (!empty($scses)) {
                  $scse = reset($scses);
                  //状态 0 待审核 1 待退款 2 已完成 3 拒绝 4 待退货
                  if ($scse instanceof SCommerceServiceLine && in_array($scse->status, [
                      0,
                      1,
                      2,
                      4,
                    ])) {
                    //运行到此处说明这个项申请了售后
                  }
                  else {
                    $lastOne = FALSE;
                  }
                }
                else {
                  $lastOne = FALSE;
                }
              }
            }
          } catch (Exception $exception) {
            watchdog_exception(__FUNCTION__ . __LINE__, $exception);
          }
        }
      }

      [
        $amount,
        $otherTotal,
        $productTotal,
        $shippingTotal,
      ] = shushengyuan_order_line_amount($order, $line);
      $refundAmount = ($amount * $line->qty) - $refund;
      if ($lastOne) {
        $refundAmount += $shippingTotal;
      }
      return [
        'code' => 0,
        'msg' => 'ok',
        'amount' => $this->convertYuan(floor($refundAmount)),
      ];
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'amount' => 0,
    ];
  }


  /**
   * @param int $type 0 全部 1 待审核 2 待退款 3 已完成
   * @param int $offset
   * @param int $limit
   * @return array
   * 店铺获取售后单
   * @throws \ServicesException
   * @OA\Get(
   *   path="/common/store_load_order_services",
   *   summary="店铺获取售后单 common/store_load_order_services",
   *   security={{"mini_token":{}},{"official_token":{}}},
   *   tags={"v1.1","vStore"},
   *   @OA\Parameter(
   *     name="type",
   *     description="状态 0 全部 1 待审核 2 待退款 3 已完成",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *          property="list",
   *          type="array",
   *          description="售后单列表",
   *          @OA\Items(ref="#/components/schemas/OrderServiceItem")
   *       ),
   *     )
   *   )
   * )
   * 店铺获取售后单
   */
  function store_load_order_services(int $type, int $offset, int $limit) {
    $store = $this->getStore();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 's_commerce_service');
    $query->propertyCondition('store_id', $store->id);
    //状态 0 全部 1 待审核 2 待退款 3 已完成
    switch ($type) {
      //状态 0 待审核 1 待退款 2 已完成 3 拒绝 4 待退货
      case 0:
        $query->propertyCondition('status', [0, 1, 2, 3, 4], 'IN');
        break;
      case 1:
        $query->propertyCondition('status', [0], 'IN');
        break;
      case 2:
        $query->propertyCondition('status', [1, 4], 'IN');
        break;
      case 3:
        $query->propertyCondition('status', [2, 3], 'IN');
        break;
      default:
        return [
          'code' => 1,
          'msg' => '请选择正确的状态',
        ];
    }
    $query->propertyOrderBy('created', 'DESC');
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['s_commerce_service'] as $item) {
        $list[] = OrderServiceItem::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @return array
   * @OA\Get(
   *   path="/common/load_sweepstake",
   *   summary="获取抽奖配置 common/load_sweepstake",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v1.0","sweepstake"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="sweepstake",
   *         type="object",
   *         ref="#/components/schemas/SweepstakeConfig",
   *         description="抽奖配置",
   *       ),
   *     )
   *   )
   * )
   */
  function load_sweepstake() {
    $es = shusy_sweepstake_load_multiple(FALSE, ['status' => 1]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusySweepstake) {
        return [
          'code' => 0,
          'msg' => 'ok',
          'sweepstake' => SweepstakeConfig::get($e->id),
        ];
      }
    }
    return [
      'code' => 1,
      'msg' => '没有配置抽奖活动',
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @throws \SearchApiException
   * @OA\Get(
   *   path="/common/loadProductPurchases",
   *   summary="获取进货商品 common/loadProductPurchases",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v2.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         @OA\Items(ref="#/components/schemas/MyStoreProductItem")
   *       ),
   *     )
   *   )
   * )
   */
  function loadProductPurchases(int $offset, int $limit) {
    $keys = '';
    $sort = [];
    $filter = [
      '~and' => [
        //显示上架的商品
        'status' => 1,
        //开启秒杀价的商品
        'purchase:status' => 1,
      ],
    ];
    $result = $this->_searchApiResourceRetrieve('products', $keys, $filter, $sort, $limit, $offset);
    $list = [];
    foreach ($result['results'] as $id => $item) {
      $list[] = ProductPurchaseItem::get($id);
    }
    $facets = [];
    foreach ($result['facets'] as $facet) {
      if (!empty($facet['items'])) {
        $facets[] = [
          'items' => $facet['items'],
          'title' => $facet['title'],
        ];
      }
    }
    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @throws \SearchApiException
   * 获取内部专享价商品
   */
  function loadTeamGoods(int $offset, int $limit) {
    $keys = '';
    $sort = [];
    $filter = [
      '~and' => [
        //显示上架的商品
        'status' => 1,
        'teamgood' => 1,
      ],
    ];
    $result = $this->_searchApiResourceRetrieve('products', $keys, $filter, $sort, $limit, $offset);
    $list = [];
    foreach ($result['results'] as $id => $item) {
      $list[] = ProductPurchaseItem::get($id);
    }
    $facets = [];
    foreach ($result['facets'] as $facet) {
      if (!empty($facet['items'])) {
        $facets[] = [
          'items' => $facet['items'],
          'title' => $facet['title'],
        ];
      }
    }
    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/loadTeamsAchievement",
   *   summary="获取团队收益记录 common/loadTeamsAchievement",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v2.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         @OA\Items(ref="#/components/schemas/MyTeamAchievement")
   *       ),
   *     )
   *   )
   * )
   */
  function loadTeamsAchievement(int $offset, int $limit) {
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'points');
    $query->entityCondition('bundle', 'achievement');
    $query->propertyCondition('uid', $uid);
    $query->propertyOrderBy('id', 'DESC');
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['points'] as $item) {
        $list[] = MyTeamAchievement::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
      'achievement' => $this->convertYuan(points_get_total($uid, 'achievement')),
    ];
  }

  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/loadInnerTeams",
   *   summary="内部团队获取和自己收益相关的人员 common/loadInnerTeams",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v2.3"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="我的内部团队",
   *         @OA\Items(ref="#/components/schemas/MyInnerTeam")
   *       ),
   *     )
   *   )
   * )
   */
  function loadInnerTeams(int $offset, int $limit) {
    $uid = $this->getUid();
    $query = db_select('shusy_team', 't');
    $query->fields('t', ['id']);
    $query->condition('status', 1);


    $db_or = db_or();
    $db_or->condition('market_1', $uid);
    $db_or->condition('market_2', $uid);
    $db_or->condition('company_1', $uid);
    $db_or->condition('company_2', $uid);

    $db_and = db_and();
    $db_and->condition($db_or);

    $query->condition($db_and);

    $query->orderBy('id', 'DESC');
    $query->range($offset, $limit);
    $result = $query->execute();
    $list = [];
    while ($row = $result->fetchAssoc()) {
      $list[] = MyInnerTeam::init($row['id']);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  function loadInnerTeamsType(int $type, int $offset, int $limit) {
    $uid = $this->getUid();
    $query = db_select('shusy_team', 't');
    $query->fields('t', ['id']);
    $query->condition('status', 1);

    $db_or = db_or();
    switch ($type) {
      case 0:
        $db_or->condition('market_1', $uid);
        $db_or->condition('market_2', $uid);
        $db_or->condition('company_1', $uid);
        $db_or->condition('company_2', $uid);
        break;
      case 1:
        //市场顾问团员
        $db_or->condition('market_1', $uid);
        break;
      case 2:
        //市场总监团员
        $db_or->condition('market_2', $uid);
        break;
      case 3:
        //分公司部长
        $db_or->condition('company_1', $uid);
        break;
    }
    $db_and = db_and();
    $db_and->condition($db_or);

    $query->condition($db_and);

    $query->orderBy('id', 'DESC');
    $query->range($offset, $limit);
    $result = $query->execute();
    $list = [];
    while ($row = $result->fetchAssoc()) {
      $list[] = MyInnerTeam::init($row['id']);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @OA\Get(
   *   path="/common/loadTeamStores",
   *   summary="查看团队推广的店铺入驻申请情况 common/loadTeamStores",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v2.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         @OA\Items(ref="#/components/schemas/MyTeamStore")
   *       ),
   *     )
   *   )
   * )
   */
  function loadTeamStores(int $offset, int $limit) {
    $uid = $this->getUid();

    $query = db_select('distribution', 'd');
    $query->innerJoin('shusy_store', 's', 'd.uid=s.uid');
    $query->fields('s', ['id']);
    $query->condition('d.pid', $uid);
    $query->range($offset, $limit);
    $query->orderBy('s.id', 'desc');

    $result = $query->execute();
    $list = [];
    while ($row = $result->fetchAssoc()) {
      $list[] = MyTeamStore::init($row['id']);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/loadGiftCards",
   *   summary="查看我的兑换卡 common/loadGiftCards",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v2.0"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         @OA\Items(ref="#/components/schemas/MyGiftCard")
   *       ),
   *     )
   *   )
   * )
   */
  function loadGiftCards(int $offset, int $limit) {
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_gift_card');
    $query->propertyCondition('uid', $uid);
    $query->propertyOrderBy('status', 'asc');
    $query->propertyOrderBy('id', 'desc');
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['shusy_gift_card'] as $item) {
        $list[] = MyGiftCard::init($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  protected function getGiftCardCounts() {
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'shusy_gift_card');
    $query->propertyCondition('uid', $uid);
    $query->propertyOrderBy('status', 'asc');
    $query->propertyOrderBy('id', 'desc');
    return $query->count()->execute();
  }

  /**
   * @param int $id
   * @return array
   * @OA\Get(
   *   path="/common/loadGiftQr",
   *   summary="获取兑换卡核销二维码 common/loadGiftQr",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v2.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="MyGiftCard.id",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="qr",
   *         type="string",
   *         description="二维码URL",
   *       ),
   *     )
   *   )
   * )
   */
  function loadGiftQr(int $id) {
    $uid = $this->getUid();
    $gift = shusy_gift_card_load($id);
    if ($gift instanceof ShusyGiftCard && $gift->uid == $uid) {
      $random = drupal_random_key();
      $cid = "{$id}.{$random}";
      $this->cacheSet($cid, $id);
      return [
        'code' => 0,
        'msg' => 'ok',
        'qr' => qr_create_code("{$id}.{$random}"),
      ];
    }
    return [
      'code' => 1,
      'msg' => '兑换卡不存在',
    ];
  }

  /**
   * @return array
   * 获取快达团队信息 用户申请加入的时候
   */
  function loadErrandTeam() {
    $uid = $this->getUid();
    $errands = shusy_errand_load_multiple(FALSE, ['uid' => $uid]);
    if (!empty($errands)) {
      $errand = reset($errands);
      if ($errand instanceof ShusyErrand) {
        if ($errand->is_admin == 1) {
          return [
            'code' => 0,
            'msg' => 'ok',
            'errand' => ErrandItem::init($errand->id),
          ];
        }
        $pid = $errand->errand_id;
        $pidErrand = shusy_errand_load($pid);
        if ($pidErrand instanceof ShusyErrand) {
          return [
            'code' => 0,
            'msg' => 'ok',
            'errand' => ErrandItem::init($pidErrand->id),
          ];
        }
      }
    }
    return [
      'code' => 1,
      'msg' => '无团队',
    ];
  }

  function loadErrandQr() {
    $uid = $this->getUid();
    return [
      'code' => 0,
      'msg' => 'ok',
      'qr' => file_create_url($this->getErrandQr($uid)),
    ];
  }

  /**
   * @param int $type
   * @param int $offset
   * @param int $limit
   * @return array
   * 获取快达团队人员
   */
  function loadErrandUsers(int $type, int $offset, int $limit) {
    $uid = $this->getUid();
    $list = [];
    $errands = shusy_errand_load_multiple(FALSE, ['uid' => $uid]);
    if (!empty($errands)) {
      $errand = reset($errands);
      if ($errand instanceof ShusyErrand) {
        $query = db_select('shusy_errand', 'l');
        $query->fields('l', ['id']);
        $query->condition('errand_id', $errand->id);
        //"全部", "已通过", "待审核"
        //状态 0 未提交 1 已通过 2 审核中 3 未通过
        switch ($type) {
          case 0:
            $query->condition('status', [1, 2], 'IN');
            break;
          case 1:
            $query->condition('status', [1], 'IN');
            break;
          case 2:
            $query->condition('status', [2], 'IN');
            break;
          default:
            return [
              'code' => 1,
              'msg' => '类型错误',
            ];
        }
        $query->orderBy('id', 'desc');
        $query->range($offset, $limit);
        $result = $query->execute();
        while ($row = $result->fetchAssoc()) {
          $list[] = ErrandItem::init($row['id']);
        }
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/loadSweepstakeLogs",
   *   summary="抽奖记录 common/loadSweepstakeLogs",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v2.4"},
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         @OA\Items(ref="#/components/schemas/SweepstakeLog")
   *       ),
   *     )
   *   )
   * )
   */
  function loadSweepstakeLogs(int $offset, int $limit) {
    $uid = $this->getUid();
    $query = db_select('shusy_sweepstake_log', 'l');
    $query->fields('l', ['id']);
    $query->condition('uid', $uid);
    $query->range($offset, $limit);
    $query->orderBy('id', 'desc');
    $result = $query->execute();
    $list = [];
    while ($row = $result->fetchAssoc()) {
      $list[] = SweepstakeLog::init($row['id']);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $id
   * @return array
   * @throws \SearchApiException
   * 推荐相关商品 相同一级分类
   */
  function loadRecommendProducts(int $id) {
    $es = shusy_product_category_load_multiple(FALSE, [
      'product_id' => $id,
      'status' => 1,
    ]);
    $depth_1 = [];
    foreach ($es as $e) {
      if ($e instanceof ShusyProductCategory) {
        $c = shusy_category_load($e->category_id);
        if ($c instanceof ShusyCategory) {
          $depth_1[$c->depth_1] = $c->depth_1;
        }
      }
    }
    $filter = [
      '~and' => [
        //显示上架的商品
        'status' => 1,
        'depth_1' => array_values($depth_1),
      ],
    ];
    $result = $this->_searchApiResourceRetrieve('products', "", $filter, [], 10, 0);
    $list = [];
    foreach ($result['results'] as $id => $item) {
      $list[] = ProductItem::get($id);
    }
    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $id
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/loadPlantGrassComments",
   *   summary="获取种草推荐的评论 common/loadPlantGrassComments",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v3.0"},
   *   @OA\Parameter(
   *     name="id",
   *     description="种草推荐ID",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="offset",
   *     description="偏移数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Parameter(
   *     name="limit",
   *     description="需要返回的数量",
   *     @OA\Schema(
   *       type="integer",
   *     ),
   *     in="query",
   *     required=true,
   *   ),
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *       @OA\Property(
   *         property="list",
   *         type="array",
   *         description="种草推荐评论",
   *         @OA\Items(ref="#/components/schemas/PlantGrassCommentsItem")
   *       ),
   *     )
   *   )
   * )
   */
  function loadPlantGrassComments(int $id, int $offset, int $limit) {
    $query = db_select('shusy_plant_grass_comments', 'c');
    $query->fields('c', ['id']);
    $query->condition('c.plant_grass_id', $id);
    $query->condition('c.status', 1);
    $query->range($offset, $limit);
    $query->orderBy('c.id', 'desc');
    $result = $query->execute();
    $list = [];
    while ($row = $result->fetchAssoc()) {
      $list[] = PlantGrassCommentsItem::init($row['id']);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   * @OA\Get(
   *   path="/common/loadMyCoupons",
   *   summary="获取我的优惠券 common/loadMyCoupons",
   *   security={{"token":{}},{"mini_token":{}},{"official_token":{}},{"app_token":{}}},
   *   tags={"v1.0"},
   *   @OA\Response(
   *     response="200",description="正常",
   *     @OA\JsonContent(
   *       @OA\Property(
   *         property="code",
   *         type="integer",
   *         description="返回码",
   *       ),
   *       @OA\Property(
   *         property="msg",
   *         type="string",
   *         description="返回信息",
   *       ),
   *     )
   *   )
   * )
   */
  function loadMyCoupons(int $type, int $offset, int $limit) {
    $uid = $this->getUid();
    $query = db_select('shusy_store_coupon_hold', 'h');
    $query->fields('h', ['id']);
    $query->condition('uid', $uid);
    switch ($type) {
      case 0:
        $query->condition('status', 0);
        $dbAnd = db_and();
        $dbAnd->condition('validity_end', REQUEST_TIME, '>=');
        $query->condition($dbAnd);
        break;
      case 1:
        $dbOr = db_or();
        $dbOr->condition('validity_end', REQUEST_TIME, '<');
        $dbOr->condition('status', 1);
        $query->condition($dbOr);
        break;
      default:
        return [
          'code' => 1,
          'msg' => '状态不正确',
        ];
    }
    $query->range($offset, $limit);
    $result = $query->execute();
    $list = [];
    while ($row = $result->fetchAssoc()) {
      $list[] = CouponHoldItem::init($row['id']);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  function loadOrderCoupons(int $id) {
    $uid = $this->getUid();
    $list = [];
    $order = shusy_store_order_load($id);
    if ($order instanceof ShusyStoreOrder) {
      $query = db_select('shusy_store_coupon_hold', 'h');
      $query->fields('h', ['id']);
      $query->condition('uid', $uid);
      $query->condition('status', 0);
      $query->condition('store_id', [0, $order->store_id], 'IN');
      $dbAnd = db_and();
      $dbAnd->condition('validity_end', REQUEST_TIME, '>=');
      $query->condition($dbAnd);
      $query->condition('condition_min', $order->amount, '<');
      $result = $query->execute();
      while ($row = $result->fetchAssoc()) {
        $list[] = CouponHoldItem::init($row['id']);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  function loadIndexCoupons() {
    $list = [];
    $query = db_select('shusy_store_coupon', 'c');
    $query->fields('c', ['id']);
    $query->condition('status', 1);
    $result = $query->execute();
    while ($row = $result->fetchAssoc()) {
      $list[] = CouponItem::init($row['id']);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  function loadProductCoupons(int $id) {
    $list = [];
    $es = shusy_couponfree_load_multiple(FALSE, ['status' => 1]);
    foreach ($es as $e) {
      if ($e instanceof ShusyCouponfree) {
        $list[] = CouponfreeItem::init($e->id, FALSE, [$id]);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  function loadGoldTip(int $id) {
    $max = 0;
    $gold = points_get_total($this->getUid(), 'gold');
    $order = shusy_store_order_load($id);
    if ($order instanceof ShusyStoreOrder) {
      $max = $this->getMaxGold($order);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'tip' => t("最多可抵扣金额:@max¥ 可用金币:@gold (@amount¥)", [
        '@max' => $this->convertYuan($max),
        '@gold' => $gold,
        '@amount' => $this->convertYuan($gold),
      ]),
    ];
  }

  protected function getMaxGold(ShusyStoreOrder $order) {
    $max = 0;
    $products = $order->products();
    foreach ($products as $line) {
      if ($line instanceof ShusyStoreOrderLine) {
        $lineWrapper = $line->wrapper();
        $gold = $lineWrapper->standard->gold->value();
        if ($gold) {
          $max += $line->total;
        }
      }
    }
    return $max;
  }

  function loadMyGolds(int $type, int $offset, int $limit) {
    $uid = $this->getUid();
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'points');
    $query->entityCondition('bundle', 'gold');
    $query->propertyCondition('uid', $uid);
    switch ($type) {
      case 0:
        $query->propertyCondition('point', 0, '>');
        break;
      case 1:
        $query->propertyCondition('point', 0, '<');
        break;
    }
    $query->propertyOrderBy('id', 'desc');
    $query->range($offset, $limit);

    $result = $query->execute();
    $list = [];
    if (!empty($result)) {
      foreach ($result['points'] as $item) {
        $list[] = PointItemGold::get($item->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  function loadAbouts(int $offset, int $limit) {
    $query = db_select('mxm_article', 'a');
    $query->fields('a', ['id']);
    $query->condition('window_index', 1);
    $query->condition('status', 1);
    $query->orderBy('sort');
    $query->orderBy('id');
    $query->range($offset, $limit);
    $result = $query->execute();
    $list = [];
    while ($row = $result->fetchAssoc()) {
      $list[] = ArticleItem::init($row['id']);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  function loadArticle(int $id) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'article' => ArticleItem::init($id),
    ];
  }

  /**
   * @param int $offset
   * @param int $limit
   * @return array
   */
  function loadHotels(int $offset, int $limit, float $latitude, float $longitude) {
    $query = db_select('shusy_hotel', 'h');
    $query->fields('h', ['id']);
    $query->condition('status', 1);
    $result = $query->execute();
    $list = [];
    while ($row = $result->fetchAssoc()) {
      $list[] = HotelItem::init($row['id']);
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => $list,
    ];
  }

  /**
   * @param int $id
   * @return array
   */
  function loadHotelDetail(int $id) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'hotel' => HotelItemDetail::init($id),
    ];
  }

  /**
   * @param int $id
   * @return array
   */
  function loadCalendarBox(int $id, string $month) {
    return [
      'code' => 0,
      'msg' => 'ok',
      'calendar' => HotelRoomCalendar::init($id, FALSE, [$month]),
    ];
  }

  /**
   * @return array
   */
  function loadStoreStats() {
    $store = $this->getStore();
    if ($store instanceof ShusyStore) {
      $query = db_select('shusy_store_stats', 's');
      $query->fields('s', ['id', 'type', 'number']);
      $query->condition('store_id', $store->id);
      $inday = strtotime(format_date(REQUEST_TIME, 'custom', 'Y-m-d'));
      $query->condition('inday', $inday);
      $result = $query->execute();
      $order = 0;
      $view = 0;
      $follow = 0;
      $refund = 0;
      while ($row = $result->fetchAssoc()) {
        switch ($row['type']) {
          case 'order':
            $order = $row['number'];
            break;
          case 'view':
            $view = $row['number'];
            break;
          case 'follow':
            $follow = $row['number'];
            break;
          case 'refund':
            $refund = $row['number'];
            break;
        }
      }
      $stats = [
        'order' => $order,
        'view' => $view,
        'follow' => $follow,
        'refund' => $refund,
      ];
      return [
        'code' => 0,
        'msg' => 'ok',
        'stats' => $stats,
      ];
    }
    return [
      'code' => 1,
      'msg' => '店铺不存在',
    ];
  }


  function loadTeamStats() {
    $list = [];
    $es = shusy_team_stats_load_multiple(FALSE);
    $total = [];
    foreach ($es as $e) {
      if ($e instanceof ShusyTeamStats) {
        $list[] = [
          'name' => $e->name,
          'offline' => '约' . $e->offline,
          'online' => '约' . $e->online,
          'total' => '约' . $e->number,
          'weight' => $e->number,
        ];
        $total['offline'] += $e->offline;
        $total['online'] += $e->online;
        $total['total'] += $e->number;
        $total['weight'] += $e->number;
      }
    }
    usort($list, 'drupal_sort_weight');
    $list = array_reverse($list);
    $total['name'] = '全部';
    $total['offline'] = '约' . $total['offline'];
    $total['online'] = '约' . $total['online'];
    $total['total'] = '约' . $total['total'];
    array_unshift($list, $total);
    $items = [];
    foreach ($list as $item) {
      $items[] = $item['name'];
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => array_values($list),
      'itemsColumns' => $items,
      'item' => reset($list),
      'imgs' => [
        'top' => 'https://shushengyuan.sccxbe.com/imgs/1.png',
        'bottom' => 'https://shushengyuan.sccxbe.com/imgs/2.png',
        'bg' => 'https://shushengyuan.sccxbe.com/imgs/t-bg.png',
        'pbg' => 'https://shushengyuan.sccxbe.com/imgs/pbg.png',
        'pf' => 'https://shushengyuan.sccxbe.com/imgs/pf.png',
        'ph' => 'https://shushengyuan.sccxbe.com/imgs/ph.png',
      ],
    ];
  }

  function loadStoreIntentionChannel() {
    return [
      'code' => 0,
      'msg' => 'ok',
      'channel' => [
        ['value' => 'online', 'name' => '线上广告'],
        ['value' => 'offline', 'name' => '线下广告'],
        ['value' => 'referral', 'name' => '朋友介绍'],
        ['value' => 'wxpengyouquan', 'name' => '微信朋友圈'],
        ['value' => 'other', 'name' => '其它'],
      ],
    ];
  }

  function loadIndexWindow() {
    $list = [];
    $es = ssy_index_window_load_multiple(FALSE, ['status' => 1]);
    foreach ($es as $e) {
      if ($e instanceof SsyIndexWindow) {
        $list[] = WindowItem::init($e->id);
      }
    }
    return [
      'code' => 0,
      'msg' => 'ok',
      'open' => !empty($es),
      'ids' => [
        'hIIvKUK8IzBFC8XgxMVQHsKw8oj7CAL_7YWC2FGRirk',
        'zvBQFXWCfKyLfTsmdYpVHMshg2IiPFz1Pzj8kZcgVh4',
        'HGp0ZE0QDBNnyM-VxeI0_zF9EaIIQzd9Hm9rsTqbmEk',
      ],
      'list' => $list,
      'popup' => reset($list),
    ];
  }

  function loadIndexProductsBox() {
    $list = [];
    $es = ssy_index_products_load_multiple(FALSE, ['status' => 1]);
    foreach ($es as $e) {
      $list[] = IndexProductsBoxItem::init($e->id);
    }
    usort($list, 'drupal_sort_weight');
    return [
      'code' => 0,
      'msg' => 'ok',
      'list' => array_values($list),
    ];
  }

  function loadGoldProducts(int $offset, int $limit) {
    $keys = '';
    $sort = [];
    $filter = [
      '~and' => [
        //显示上架的商品
        'status' => 1,
        'is_gold' => 1,
      ],
    ];
    $result = $this->_searchApiResourceRetrieve('products', $keys, $filter, $sort, $limit, $offset);
    $list = [];
    foreach ($result['results'] as $id => $item) {
      $list[] = ProductPurchaseItem::get($id);
    }
    $facets = [];
    foreach ($result['facets'] as $facet) {
      if (!empty($facet['items'])) {
        $facets[] = [
          'items' => $facet['items'],
          'title' => $facet['title'],
        ];
      }
    }
    return [
      'code' => (int) 0,
      'msg' => (string) 'ok',
      'list' => $list,
    ];
  }

  function loadGoldExchangeIndex() {
    $ruleid = variable_get_value('shushengyuan:article:goldrule:id');
    $uid = $this->getUid();
    return [
      'code' => 0,
      'msg' => 'ok',
      'user' => User::init($uid),
      'gold' => [
        "bg" => 'https://shushengyuan.sccxbe.com/imgs/gold/bg.png',
        'gold' => points_get_total($this->getUid(), 'gold'),
        'ijb' => 'https://shushengyuan.sccxbe.com/imgs/gold/i-jb.png',
        'igx' => 'https://shushengyuan.sccxbe.com/imgs/gold/i-gx.png',
        'isr' => 'https://shushengyuan.sccxbe.com/imgs/gold/i-sr.png',
        'rule' => "/pages/article/article?id={$ruleid}",
      ],
    ];
  }

  function loadMyTeamIndex() {
    $uid = $this->getUid();
    return [
      'code' => 0,
      'msg' => 'ok',
      'user' => User::init($uid),
      'team' => [
        'bg' => 'https://shushengyuan.sccxbe.com/imgs/team/bg.png',
        //提现详情
        'txxq' => '/pages/my/integral/integralsubsidiary/integralsubsidiary?type=3',
        'amount' => t("@amount 元", ['@amount' => $this->convertYuan(points_get_total($uid, 'income'))]),
        'iscgwty' => 'https://shushengyuan.sccxbe.com/imgs/team/iscgwty.png',
        'isczjty' => 'https://shushengyuan.sccxbe.com/imgs/team/isczjty.png',
        'ifgsbz' => 'https://shushengyuan.sccxbe.com/imgs/team/ifgsbz.png',
        'idprzqk' => 'https://shushengyuan.sccxbe.com/imgs/team/idprzqk.png',
        'idptgm' => 'https://shushengyuan.sccxbe.com/imgs/team/ishgx.png',
        'itdtgm' => 'https://shushengyuan.sccxbe.com/imgs/team/itdtgm.png',
        'inbzxq' => 'https://shushengyuan.sccxbe.com/imgs/team/inbzxq.png',
        'iqysj' => 'https://shushengyuan.sccxbe.com/imgs/team/iqysj.png',
        'izs' => 'https://shushengyuan.sccxbe.com/imgs/team/zs.png',
      ],
    ];
  }

}
